runMACS
 All Data Structures Files Functions Variables Enumerations Enumerator Macros
portable_endian.h
Go to the documentation of this file.
1 #ifndef PORTABLE_ENDIAN_H__
2 #define PORTABLE_ENDIAN_H__
3 
4 #if (defined(_WIN16) || defined(_WIN32) || defined(_WIN64)) && !defined(__WINDOWS__)
5 
6 # define __WINDOWS__
7 
8 #endif
9 
10 #if defined(__linux__) || defined(__CYGWIN__)
11 
12 # include <endian.h>
13 
14 #elif defined(__APPLE__)
15 
16 # include <libkern/OSByteOrder.h>
17 
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
30 
31 # define __BYTE_ORDER BYTE_ORDER
32 # define __BIG_ENDIAN BIG_ENDIAN
33 # define __LITTLE_ENDIAN LITTLE_ENDIAN
34 # define __PDP_ENDIAN PDP_ENDIAN
35 
36 #elif defined(__OpenBSD__)
37 
38 # include <sys/endian.h>
39 
40 #elif defined(__NetBSD__) || defined(__FreeBSD__) || defined(__DragonFly__)
41 
42 # include <sys/endian.h>
43 
44 # define be16toh betoh16
45 # define le16toh letoh16
46 
47 # define be32toh betoh32
48 # define le32toh letoh32
49 
50 # define be64toh betoh64
51 # define le64toh letoh64
52 
53 #elif defined(__WINDOWS__)
54 
55 # include <winsock2.h>
56 # include <sys/param.h>
57 
58 # if BYTE_ORDER == LITTLE_ENDIAN
59 
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)
72 
73 # elif BYTE_ORDER == BIG_ENDIAN
74 
75 /* that would be xbox 360 */
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)
88 
89 # else
90 
91 # error byte order not supported
92 
93 # endif
94 
95 # define __BYTE_ORDER BYTE_ORDER
96 # define __BIG_ENDIAN BIG_ENDIAN
97 # define __LITTLE_ENDIAN LITTLE_ENDIAN
98 # define __PDP_ENDIAN PDP_ENDIAN
99 
100 #else
101 
102 # error platform not supported
103 
104 #endif
105 
106 #endif