gwenhywfar  5.11.2beta
portable_endian.h
Go to the documentation of this file.
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 #ifndef PORTABLE_ENDIAN_H__
8 #define PORTABLE_ENDIAN_H__
9 
10 #if (defined(_WIN16) || defined(_WIN32) || defined(_WIN64)) && !defined(__WINDOWS__)
11 
12 # define __WINDOWS__
13 
14 #endif
15 
16 #if defined(__linux__) || defined(__CYGWIN__)
17 
18 # include <endian.h>
19 
20 #elif defined(__APPLE__)
21 
22 # include <libkern/OSByteOrder.h>
23 
24 # define htobe16(x) OSSwapHostToBigInt16(x)
25 # define htole16(x) OSSwapHostToLittleInt16(x)
26 # define be16toh(x) OSSwapBigToHostInt16(x)
27 # define le16toh(x) OSSwapLittleToHostInt16(x)
28 
29 # define htobe32(x) OSSwapHostToBigInt32(x)
30 # define htole32(x) OSSwapHostToLittleInt32(x)
31 # define be32toh(x) OSSwapBigToHostInt32(x)
32 # define le32toh(x) OSSwapLittleToHostInt32(x)
33 
34 # define htobe64(x) OSSwapHostToBigInt64(x)
35 # define htole64(x) OSSwapHostToLittleInt64(x)
36 # define be64toh(x) OSSwapBigToHostInt64(x)
37 # define le64toh(x) OSSwapLittleToHostInt64(x)
38 
39 # define __BYTE_ORDER BYTE_ORDER
40 # define __BIG_ENDIAN BIG_ENDIAN
41 # define __LITTLE_ENDIAN LITTLE_ENDIAN
42 # define __PDP_ENDIAN PDP_ENDIAN
43 
44 #elif defined(__OpenBSD__)
45 
46 # include <endian.h>
47 
48 # define __BYTE_ORDER BYTE_ORDER
49 # define __BIG_ENDIAN BIG_ENDIAN
50 # define __LITTLE_ENDIAN LITTLE_ENDIAN
51 # define __PDP_ENDIAN PDP_ENDIAN
52 
53 #elif defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__)
54 
55 # include <sys/endian.h>
56 
57 # define be16toh(x) betoh16(x)
58 # define le16toh(x) letoh16(x)
59 
60 # define be32toh(x) betoh32(x)
61 # define le32toh(x) letoh32(x)
62 
63 # define be64toh(x) betoh64(x)
64 # define le64toh(x) letoh64(x)
65 
66 #elif defined(__WINDOWS__)
67 
68 # include <winsock2.h>
69 # ifdef __GNUC__
70 # include <sys/param.h>
71 # endif
72 
73 # if BYTE_ORDER == LITTLE_ENDIAN
74 
75 # define htobe16(x) htons(x)
76 # define htole16(x) (x)
77 # define be16toh(x) ntohs(x)
78 # define le16toh(x) (x)
79 
80 # define htobe32(x) htonl(x)
81 # define htole32(x) (x)
82 # define be32toh(x) ntohl(x)
83 # define le32toh(x) (x)
84 
85 # define htobe64(x) htonll(x)
86 # define htole64(x) (x)
87 # define be64toh(x) ntohll(x)
88 # define le64toh(x) (x)
89 
90 # elif BYTE_ORDER == BIG_ENDIAN
91 
92  /* that would be xbox 360 */
93 # define htobe16(x) (x)
94 # define htole16(x) __builtin_bswap16(x)
95 # define be16toh(x) (x)
96 # define le16toh(x) __builtin_bswap16(x)
97 
98 # define htobe32(x) (x)
99 # define htole32(x) __builtin_bswap32(x)
100 # define be32toh(x) (x)
101 # define le32toh(x) __builtin_bswap32(x)
102 
103 # define htobe64(x) (x)
104 # define htole64(x) __builtin_bswap64(x)
105 # define be64toh(x) (x)
106 # define le64toh(x) __builtin_bswap64(x)
107 
108 # else
109 
110 # error byte order not supported
111 
112 # endif
113 
114 # define __BYTE_ORDER BYTE_ORDER
115 # define __BIG_ENDIAN BIG_ENDIAN
116 # define __LITTLE_ENDIAN LITTLE_ENDIAN
117 # define __PDP_ENDIAN PDP_ENDIAN
118 
119 #elif defined(__QNXNTO__)
120 
121 # include <gulliver.h>
122 
123 # define __LITTLE_ENDIAN 1234
124 # define __BIG_ENDIAN 4321
125 # define __PDP_ENDIAN 3412
126 
127 # if defined(__BIGENDIAN__)
128 
129 # define __BYTE_ORDER __BIG_ENDIAN
130 
131 # define htobe16(x) (x)
132 # define htobe32(x) (x)
133 # define htobe64(x) (x)
134 
135 # define htole16(x) ENDIAN_SWAP16(x)
136 # define htole32(x) ENDIAN_SWAP32(x)
137 # define htole64(x) ENDIAN_SWAP64(x)
138 
139 # elif defined(__LITTLEENDIAN__)
140 
141 # define __BYTE_ORDER __LITTLE_ENDIAN
142 
143 # define htole16(x) (x)
144 # define htole32(x) (x)
145 # define htole64(x) (x)
146 
147 # define htobe16(x) ENDIAN_SWAP16(x)
148 # define htobe32(x) ENDIAN_SWAP32(x)
149 # define htobe64(x) ENDIAN_SWAP64(x)
150 
151 # else
152 
153 # error byte order not supported
154 
155 # endif
156 
157 # define be16toh(x) ENDIAN_BE16(x)
158 # define be32toh(x) ENDIAN_BE32(x)
159 # define be64toh(x) ENDIAN_BE64(x)
160 # define le16toh(x) ENDIAN_LE16(x)
161 # define le32toh(x) ENDIAN_LE32(x)
162 # define le64toh(x) ENDIAN_LE64(x)
163 
164 #else
165 
166 # error platform not supported
167 
168 #endif
169 
170 #endif