4 // https://gist.github.com/panzi/6856583
6 // I, Mathias Panzenböck, place this file hereby into the public domain. Use
7 // it at your own risk for whatever you like. In case there are
8 // jurisdictions that don't support putting things in the public domain you
9 // can also consider it to be "dual licensed" under the BSD, MIT and Apache
10 // licenses, if you want to. This code is trivial anyway. Consider it an
11 // example on how to get the endian conversion functions on different
14 #ifndef PORTABLE_ENDIAN_H__
15 #define PORTABLE_ENDIAN_H__
17 #if (defined(_WIN16) || defined(_WIN32) || defined(_WIN64)) && !defined(__WINDOWS__)
23 #if defined(__linux__) || defined(__CYGWIN__)
25 # include_next <endian.h>
27 #elif defined(__APPLE__)
29 # include <libkern/OSByteOrder.h>
31 # define htobe16(x) OSSwapHostToBigInt16(x)
32 # define htole16(x) OSSwapHostToLittleInt16(x)
33 # define be16toh(x) OSSwapBigToHostInt16(x)
34 # define le16toh(x) OSSwapLittleToHostInt16(x)
36 # define htobe32(x) OSSwapHostToBigInt32(x)
37 # define htole32(x) OSSwapHostToLittleInt32(x)
38 # define be32toh(x) OSSwapBigToHostInt32(x)
39 # define le32toh(x) OSSwapLittleToHostInt32(x)
41 # define htobe64(x) OSSwapHostToBigInt64(x)
42 # define htole64(x) OSSwapHostToLittleInt64(x)
43 # define be64toh(x) OSSwapBigToHostInt64(x)
44 # define le64toh(x) OSSwapLittleToHostInt64(x)
46 # define __BYTE_ORDER BYTE_ORDER
47 # define __BIG_ENDIAN BIG_ENDIAN
48 # define __LITTLE_ENDIAN LITTLE_ENDIAN
49 # define __PDP_ENDIAN PDP_ENDIAN
51 #elif defined(__OpenBSD__) || defined(__FreeBSD__)
53 # include <sys/endian.h>
55 #elif defined(__NetBSD__) || defined(__DragonFly__)
57 # include <sys/endian.h>
59 # define be16toh(x) betoh16(x)
60 # define le16toh(x) letoh16(x)
62 # define be32toh(x) betoh32(x)
63 # define le32toh(x) letoh32(x)
65 # define be64toh(x) betoh64(x)
66 # define le64toh(x) letoh64(x)
68 #elif defined(__WINDOWS__)
70 # include <winsock2.h>
71 # include <sys/param.h>
73 # if BYTE_ORDER == LITTLE_ENDIAN
75 # define htobe16(x) __builtin_bswap16(x)
76 # define htole16(x) (x)
77 # define be16toh(x) __builtin_bswap16(x)
78 # define le16toh(x) (x)
80 # define htobe32(x) __builtin_bswap32(x)
81 # define htole32(x) (x)
82 # define be32toh(x) __builtin_bswap32(x)
83 # define le32toh(x) (x)
85 # define htobe64(x) __builtin_bswap64(x)
86 # define htole64(x) (x)
87 # define be64toh(x) __builtin_bswap64(x)
88 # define le64toh(x) (x)
90 # elif BYTE_ORDER == BIG_ENDIAN
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)
98 # define htobe32(x) (x)
99 # define htole32(x) __builtin_bswap32(x)
100 # define be32toh(x) (x)
101 # define le32toh(x) __builtin_bswap32(x)
103 # define htobe64(x) (x)
104 # define htole64(x) __builtin_bswap64(x)
105 # define be64toh(x) (x)
106 # define le64toh(x) __builtin_bswap64(x)
110 # error byte order not supported
114 # define __BYTE_ORDER BYTE_ORDER
115 # define __BIG_ENDIAN BIG_ENDIAN
116 # define __LITTLE_ENDIAN LITTLE_ENDIAN
117 # define __PDP_ENDIAN PDP_ENDIAN
121 # error platform not supported