Go to the documentation of this file. 1 #ifndef PORTABLE_ENDIAN_H__
2 #define PORTABLE_ENDIAN_H__
4 #if (defined(_WIN16) || defined(_WIN32) || defined(_WIN64)) && !defined(__WINDOWS__)
10 #if defined(__linux__) || defined(__CYGWIN__)
14 #elif defined(__APPLE__)
16 # include <libkern/OSByteOrder.h>
18 # define htobe16 OSSwapHostToBigInt16
19 # define htole16 OSSwapHostToLittleInt16
20 # define be16toh OSSwapBigToHostInt16
21 # define le16toh OSSwapLittleToHostInt16
22 # define htobe32 OSSwapHostToBigInt32
23 # define htole32 OSSwapHostToLittleInt32
24 # define be32toh OSSwapBigToHostInt32
25 # define le32toh OSSwapLittleToHostInt32
26 # define htobe64 OSSwapHostToBigInt64
27 # define htole64 OSSwapHostToLittleInt64
28 # define be64toh OSSwapBigToHostInt64
29 # define le64toh OSSwapLittleToHostInt64
31 # define __BYTE_ORDER BYTE_ORDER
32 # define __BIG_ENDIAN BIG_ENDIAN
33 # define __LITTLE_ENDIAN LITTLE_ENDIAN
34 # define __PDP_ENDIAN PDP_ENDIAN
36 #elif defined(__OpenBSD__)
38 # include <sys/endian.h>
40 #elif defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__)
42 # include <sys/endian.h>
44 # define be16toh betoh16
45 # define le16toh letoh16
47 # define be32toh betoh32
48 # define le32toh letoh32
50 # define be64toh betoh64
51 # define le64toh letoh64
53 #elif defined(__WINDOWS__)
55 # include <winsock2.h>
56 # include <sys/param.h>
58 # if BYTE_ORDER == LITTLE_ENDIAN
60 # define htobe16 htons
61 # define htole16(x) (x)
62 # define be16toh ntohs
63 # define le16toh(x) (x)
64 # define htobe32 htonl
65 # define htole32(x) (x)
66 # define be32toh ntohl
67 # define le32toh(x) (x)
68 # define htobe64(x) __builtin_bswap64(x)
69 # define htole64(x) (x)
70 # define be64toh(x) __builtin_bswap64(x)
71 # define le64toh(x) (x)
73 # elif BYTE_ORDER == BIG_ENDIAN
76 # define htobe16(x) (x)
77 # define htole16(x) __builtin_bswap16(x)
78 # define be16toh(x) (x)
79 # define le16toh(x) __builtin_bswap16(x)
80 # define htobe32(x) (x)
81 # define htole32(x) __builtin_bswap32(x)
82 # define be32toh(x) (x)
83 # define le32toh(x) __builtin_bswap32(x)
84 # define htobe64(x) (x)
85 # define htole64(x) __builtin_bswap64(x)
86 # define be64toh(x) (x)
87 # define le64toh(x) __builtin_bswap64(x)
91 # error byte order not supported
95 # define __BYTE_ORDER BYTE_ORDER
96 # define __BIG_ENDIAN BIG_ENDIAN
97 # define __LITTLE_ENDIAN LITTLE_ENDIAN
98 # define __PDP_ENDIAN PDP_ENDIAN
102 # error platform not supported