4 /* Public domain implementation for endianness detection and byte ordering on
5 several platforms. In case the concept of public domain does not exist
6 under your jurisdiction, you can consider it to be dual licensed
7 under the MIT, Apache and WTFPL licenses.
9 Grab it and drop it into your project, include it and use
10 the following macros to determine endianness:
12 ENDIANNESS_LE, ENDIANNESS_BE
14 e.g. #if ENDIANNESS_LE ...
16 or, even nicer without littering your code with #ifdefs:
18 if (ENDIANNESS_BE) { big_endian_code(); } else { little_endian_code(); }
20 ... since the compiler can optimize away unused branches, this makes your
21 code easier to read while not loosing any of the advantage of using
22 conditional compilation, plus you get a free compile-time check of the
23 unused code path (rarely used conditonally compiled code paths often get
24 defunct over time if nobody checks them all the time).
26 To debug this header yourself, you can define ENDIANNESS_DEBUG to see
27 warnings from where we take the defs for the specific target.
29 If you need only the conversion functions from big to little endian
30 and vice versa, you may want to #define ENDIANNESS_PORTABLE_CONVERSION
31 prior to including this header. That way, when the endiannes can't be
32 determined at compile time, the code will fallback to a slower,
33 but portable version of those functions.
34 However, if using it, it's not guaranteed that ENDIANNESS_LE/BE
36 Most people however need only the conversion functions in their code,
37 so if you stick to them you can safely turn the portable conversion on.
40 /* This should catch all modern GCCs and Clang */
41 #if (defined __BYTE_ORDER__) && (defined __ORDER_LITTLE_ENDIAN__)
42 # ifdef ENDIANNESS_DEBUG
43 # warning "Taking endiannes from built-in __BYTE_ORDER__"
45 # if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
46 # define ENDIANNESS_LE 1
47 # define ENDIANNESS_BE 0
48 # elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
49 # define ENDIANNESS_LE 0
50 # define ENDIANNESS_BE 1
52 /* Try to derive from arch/compiler-specific macros */
53 #elif defined(_X86_) || defined(__x86_64__) || defined(__i386__) || \
54 defined(__i486__) || defined(__i586__) || defined(__i686__) || \
55 defined(__MIPSEL) || defined(_MIPSEL) || defined(MIPSEL) || \
56 defined(__ARMEL__) || \
57 (defined(__LITTLE_ENDIAN__) && __LITTLE_ENDIAN__ == 1) || \
58 (defined(_LITTLE_ENDIAN) && _LITTLE_ENDIAN == 1) || \
59 defined(_M_IX86) || defined(_M_AMD64) /* MSVC */
60 # ifdef ENDIANNESS_DEBUG
61 # warning "Detected Little Endian target CPU"
63 # define ENDIANNESS_LE 1
64 # define ENDIANNESS_BE 0
65 #elif defined(__MIPSEB) || defined(_MIPSEB) || defined(MIPSEB) || \
66 defined(__MICROBLAZEEB__) || defined(__ARMEB__) || \
67 (defined(__BIG_ENDIAN__) && __BIG_ENDIAN__ == 1) || \
68 (defined(_BIG_ENDIAN) && _BIG_ENDIAN == 1)
69 # ifdef ENDIANNESS_DEBUG
70 # warning "Detected Big Endian target CPU"
72 # define ENDIANNESS_LE 0
73 # define ENDIANNESS_BE 1
74 /* Try to get it from a header */
77 # ifdef ENDIANNESS_DEBUG
78 # warning "Taking endiannes from endian.h"
82 # ifdef ENDIANNESS_DEBUG
83 # warning "Taking endiannes from machine/endian.h"
85 # include <machine/endian.h>
91 # if defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN)
92 # if __BYTE_ORDER == __LITTLE_ENDIAN
93 # define ENDIANNESS_LE 1
94 # define ENDIANNESS_BE 0
95 # elif __BYTE_ORDER == __BIG_ENDIAN
96 # define ENDIANNESS_LE 0
97 # define ENDIANNESS_BE 1
99 # elif defined(BYTE_ORDER) && defined(LITTLE_ENDIAN)
100 # if BYTE_ORDER == LITTLE_ENDIAN
101 # define ENDIANNESS_LE 1
102 # define ENDIANNESS_BE 0
103 # elif BYTE_ORDER == BIG_ENDIAN
104 # define ENDIANNESS_LE 0
105 # define ENDIANNESS_BE 1
110 /* In case the user passed one of -DENDIANNESS_LE or BE in CPPFLAS,
111 set the second one too */
112 #if defined(ENDIANNESS_LE) && !(defined(ENDIANNESS_BE))
113 # if ENDIANNESS_LE == 0
114 # define ENDIANNESS_BE 1
116 # define ENDIANNESS_BE 0
118 #elif defined(ENDIANNESS_BE) && !(defined(ENDIANNESS_LE))
119 # if ENDIANNESS_BE == 0
120 # define ENDIANNESS_LE 1
122 # define ENDIANNESS_LE 0
126 #if !(defined(ENDIANNESS_LE)) && !(defined(ENDIANNESS_PORTABLE_CONVERSION))
127 # error "Sorry, we couldn't detect endiannes for your system! Please set -DENDIANNESS_LE=1 or 0 using your CPPFLAGS/CFLAGS and open an issue for your system on https://github.com/rofl0r/endianness.h - Thanks!"
130 #ifdef ENDIANNESS_DEBUG
131 # if ENDIANNESS_LE == 1
132 # warning "Detected Little Endian target CPU"
134 # if ENDIANNESS_BE == 1
135 # warning "Detected BIG Endian target CPU"
142 static __inline
uint16_t end_bswap16(uint16_t __x
)
144 return (__x
<<8) | (__x
>>8);
147 static __inline
uint32_t end_bswap32(uint32_t __x
)
149 return (__x
>>24) | (__x
>>8&0xff00) | (__x
<<8&0xff0000) | (__x
<<24);
152 static __inline
uint64_t end_bswap64(uint64_t __x
)
154 return ((end_bswap32(__x
)+0ULL)<<32) | (end_bswap32(__x
>>32));
157 static __inline
uint16_t end_net2host16(uint16_t net_number
)
161 for (i
= 0; i
< (int)sizeof(result
); i
++) {
163 result
+= (((unsigned char *)&net_number
)[i
] & UCHAR_MAX
);
168 static __inline
uint16_t end_host2net16(uint16_t native_number
)
172 for (i
= (int)sizeof(result
) - 1; i
>= 0; i
--) {
173 ((unsigned char *)&result
)[i
] = native_number
& UCHAR_MAX
;
174 native_number
>>= CHAR_BIT
;
179 static __inline
uint32_t end_net2host32(uint32_t net_number
)
183 for (i
= 0; i
< (int)sizeof(result
); i
++) {
185 result
+= (((unsigned char *)&net_number
)[i
] & UCHAR_MAX
);
190 static __inline
uint32_t end_host2net32(uint32_t native_number
)
194 for (i
= (int)sizeof(result
) - 1; i
>= 0; i
--) {
195 ((unsigned char *)&result
)[i
] = native_number
& UCHAR_MAX
;
196 native_number
>>= CHAR_BIT
;
201 static __inline
uint64_t end_net2host64(uint64_t net_number
)
205 for (i
= 0; i
< (int)sizeof(result
); i
++) {
207 result
+= (((unsigned char *)&net_number
)[i
] & UCHAR_MAX
);
212 static __inline
uint64_t end_host2net64(uint64_t native_number
)
216 for (i
= (int)sizeof(result
) - 1; i
>= 0; i
--) {
217 ((unsigned char *)&result
)[i
] = native_number
& UCHAR_MAX
;
218 native_number
>>= CHAR_BIT
;
223 #if ENDIANNESS_LE+0 == 1
224 # define end_htobe16(x) end_bswap16(x)
225 # define end_be16toh(x) end_bswap16(x)
226 # define end_htobe32(x) end_bswap32(x)
227 # define end_be32toh(x) end_bswap32(x)
228 # define end_htobe64(x) end_bswap64(x)
229 # define end_be64toh(x) end_bswap64(x)
230 # define end_htole16(x) (uint16_t)(x)
231 # define end_le16toh(x) (uint16_t)(x)
232 # define end_htole32(x) (uint32_t)(x)
233 # define end_le32toh(x) (uint32_t)(x)
234 # define end_htole64(x) (uint64_t)(x)
235 # define end_le64toh(x) (uint64_t)(x)
236 #elif ENDIANNESS_BE+0 == 1
237 # define end_htobe16(x) (uint16_t)(x)
238 # define end_be16toh(x) (uint16_t)(x)
239 # define end_htobe32(x) (uint32_t)(x)
240 # define end_be32toh(x) (uint32_t)(x)
241 # define end_htobe64(x) (uint64_t)(x)
242 # define end_be64toh(x) (uint64_t)(x)
243 # define end_htole16(x) end_bswap16(x)
244 # define end_le16toh(x) end_bswap16(x)
245 # define end_htole32(x) end_bswap32(x)
246 # define end_le32toh(x) end_bswap32(x)
247 # define end_htole64(x) end_bswap64(x)
248 # define end_le64toh(x) end_bswap64(x)
250 /* Resort to slower, but neutral code */
251 # define end_htobe16(x) end_host2net16(x)
252 # define end_be16toh(x) end_net2host16(x)
253 # define end_htobe32(x) end_host2net32(x)
254 # define end_be32toh(x) end_net2host32(x)
255 # define end_htobe64(x) end_host2net64(x)
256 # define end_be64toh(x) end_net2host64(x)
257 # define end_htole16(x) end_bswap_16(end_host2net16(x))
258 # define end_le16toh(x) end_bswap_16(end_host2net16(x))
259 # define end_htole32(x) end_bswap_32(end_host2net32(x))
260 # define end_le32toh(x) end_bswap_32(end_host2net32(x))
261 # define end_htole64(x) end_bswap_64(end_host2net64(x))
262 # define end_le64toh(x) end_bswap_64(end_host2net64(x))
265 #define end_ntoh16(x) end_be16toh(x)
266 #define end_hton16(x) end_htobe16(x)
267 #define end_ntoh32(x) end_be32toh(x)
268 #define end_hton32(x) end_htobe32(x)
269 #define end_ntoh64(x) end_be64toh(x)
270 #define end_hton64(x) end_htobe64(x)