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:
4: #ifndef PORTABLE_ENDIAN_H__
5: #define PORTABLE_ENDIAN_H__
6:
7: #if (defined(_WIN16) || defined(_WIN32) || defined(_WIN64)) && !defined(__WINDOWS__)
8:
9: # define __WINDOWS__
10:
11: #endif
12:
13: #if defined(__CYGWIN__)
14:
15: # include <endian.h>
16:
17: #elif defined(HAVE_ENDIAN_H)
18: # include <endian.h>
19:
20: #elif defined(HAVE_SYS_ENDIAN_H)
21: # include <sys/endian.h>
22:
23: # if defined(__sgi)
24:
25: # include <netinet/in.h>
26: # include <inttypes.h>
27:
28: # define be64toh(x) (x)
29: # define htobe64(x) (x)
30:
31: # endif
32:
33: #elif defined(__APPLE__)
34:
35: # include <libkern/OSByteOrder.h>
36:
37: # define htobe16(x) OSSwapHostToBigInt16(x)
38: # define htole16(x) OSSwapHostToLittleInt16(x)
39: # define be16toh(x) OSSwapBigToHostInt16(x)
40: # define le16toh(x) OSSwapLittleToHostInt16(x)
41:
42: # define htobe32(x) OSSwapHostToBigInt32(x)
43: # define htole32(x) OSSwapHostToLittleInt32(x)
44: # define be32toh(x) OSSwapBigToHostInt32(x)
45: # define le32toh(x) OSSwapLittleToHostInt32(x)
46:
47: # define htobe64(x) OSSwapHostToBigInt64(x)
48: # define htole64(x) OSSwapHostToLittleInt64(x)
49: # define be64toh(x) OSSwapBigToHostInt64(x)
50: # define le64toh(x) OSSwapLittleToHostInt64(x)
51:
52: # define __BYTE_ORDER BYTE_ORDER
53: # define __BIG_ENDIAN BIG_ENDIAN
54: # define __LITTLE_ENDIAN LITTLE_ENDIAN
55: # define __PDP_ENDIAN PDP_ENDIAN
56:
57: #elif defined(__sun) && defined(__SVR4)
58:
59: # include <sys/types.h>
60: # include <netinet/in.h>
61: # include <inttypes.h>
62:
63: # if !defined (ntohll) || !defined(htonll)
64: # ifdef _BIG_ENDIAN
65: # define htonll(x) (x)
66: # define ntohll(x) (x)
67: # else
68: # define htonll(x) ((((uint64_t)htonl(x)) << 32) + htonl((uint64_t)(x) >> 32))
69: # define ntohll(x) ((((uint64_t)ntohl(x)) << 32) + ntohl((uint64_t)(x) >> 32))
70: # endif
71: # endif
72:
73: # define be64toh(x) ntohll(x)
74: # define htobe64(x) htonll(x)
75:
76: #elif defined(__WINDOWS__)
77:
78: # include <winsock2.h>
79: # include <sys/param.h>
80:
81: # if BYTE_ORDER == LITTLE_ENDIAN
82:
83: # define htobe16(x) htons(x)
84: # define htole16(x) (x)
85: # define be16toh(x) ntohs(x)
86: # define le16toh(x) (x)
87:
88: # define htobe32(x) htonl(x)
89: # define htole32(x) (x)
90: # define be32toh(x) ntohl(x)
91: # define le32toh(x) (x)
92:
93: # define htobe64(x) htonll(x)
94: # define htole64(x) (x)
95: # define be64toh(x) ntohll(x)
96: # define le64toh(x) (x)
97:
98: # elif BYTE_ORDER == BIG_ENDIAN
99:
100: /* that would be xbox 360 */
101: # define htobe16(x) (x)
102: # define htole16(x) __builtin_bswap16(x)
103: # define be16toh(x) (x)
104: # define le16toh(x) __builtin_bswap16(x)
105:
106: # define htobe32(x) (x)
107: # define htole32(x) __builtin_bswap32(x)
108: # define be32toh(x) (x)
109: # define le32toh(x) __builtin_bswap32(x)
110:
111: # define htobe64(x) (x)
112: # define htole64(x) __builtin_bswap64(x)
113: # define be64toh(x) (x)
114: # define le64toh(x) __builtin_bswap64(x)
115:
116: # else
117:
118: # error byte order not supported
119:
120: # endif
121:
122: # define __BYTE_ORDER BYTE_ORDER
123: # define __BIG_ENDIAN BIG_ENDIAN
124: # define __LITTLE_ENDIAN LITTLE_ENDIAN
125: # define __PDP_ENDIAN PDP_ENDIAN
126:
127: #else
128:
129: // Unsupported platforms.
130: // Intended to support CentOS 5 but hopefully not too far from
131: // the truth because we use the homebrew htonll, et al. implementations
132: // that were originally the sole implementation of this functionality
133: // in iperf 3.0.
134: # warning platform not supported
135: # include <endian.h>
136: #if BYTE_ORDER == BIG_ENDIAN
137: #define HTONLL(n) (n)
138: #define NTOHLL(n) (n)
139: #else
140: #define HTONLL(n) ((((unsigned long long)(n) & 0xFF) << 56) | \
141: (((unsigned long long)(n) & 0xFF00) << 40) | \
142: (((unsigned long long)(n) & 0xFF0000) << 24) | \
143: (((unsigned long long)(n) & 0xFF000000) << 8) | \
144: (((unsigned long long)(n) & 0xFF00000000) >> 8) | \
145: (((unsigned long long)(n) & 0xFF0000000000) >> 24) | \
146: (((unsigned long long)(n) & 0xFF000000000000) >> 40) | \
147: (((unsigned long long)(n) & 0xFF00000000000000) >> 56))
148:
149: #define NTOHLL(n) ((((unsigned long long)(n) & 0xFF) << 56) | \
150: (((unsigned long long)(n) & 0xFF00) << 40) | \
151: (((unsigned long long)(n) & 0xFF0000) << 24) | \
152: (((unsigned long long)(n) & 0xFF000000) << 8) | \
153: (((unsigned long long)(n) & 0xFF00000000) >> 8) | \
154: (((unsigned long long)(n) & 0xFF0000000000) >> 24) | \
155: (((unsigned long long)(n) & 0xFF000000000000) >> 40) | \
156: (((unsigned long long)(n) & 0xFF00000000000000) >> 56))
157: #endif
158:
159: #define htobe64(n) HTONLL(n)
160: #define be64toh(n) NTOHLL(n)
161:
162: #endif
163:
164: #endif
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>