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