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!"
133 static __inline
uint16_t end_bswap16(uint16_t __x
)
135 return (__x
<<8) | (__x
>>8);
138 static __inline
uint32_t end_bswap32(uint32_t __x
)
140 return (__x
>>24) | (__x
>>8&0xff00) | (__x
<<8&0xff0000) | (__x
<<24);
143 static __inline
uint64_t end_bswap64(uint64_t __x
)
145 return ((end_bswap32(__x
)+0ULL)<<32) | (end_bswap32(__x
>>32));
148 static __inline
uint16_t end_net2host16(uint16_t net_number
)
152 for (i
= 0; i
< (int)sizeof(result
); i
++) {
154 result
+= (((unsigned char *)&net_number
)[i
] & UCHAR_MAX
);
159 static __inline
uint16_t end_host2net16(uint16_t native_number
)
163 for (i
= (int)sizeof(result
) - 1; i
>= 0; i
--) {
164 ((unsigned char *)&result
)[i
] = native_number
& UCHAR_MAX
;
165 native_number
>>= CHAR_BIT
;
170 static __inline
uint32_t end_net2host32(uint32_t net_number
)
174 for (i
= 0; i
< (int)sizeof(result
); i
++) {
176 result
+= (((unsigned char *)&net_number
)[i
] & UCHAR_MAX
);
181 static __inline
uint32_t end_host2net32(uint32_t native_number
)
185 for (i
= (int)sizeof(result
) - 1; i
>= 0; i
--) {
186 ((unsigned char *)&result
)[i
] = native_number
& UCHAR_MAX
;
187 native_number
>>= CHAR_BIT
;
192 static __inline
uint64_t end_net2host64(uint64_t net_number
)
196 for (i
= 0; i
< (int)sizeof(result
); i
++) {
198 result
+= (((unsigned char *)&net_number
)[i
] & UCHAR_MAX
);
203 static __inline
uint64_t end_host2net64(uint64_t native_number
)
207 for (i
= (int)sizeof(result
) - 1; i
>= 0; i
--) {
208 ((unsigned char *)&result
)[i
] = native_number
& UCHAR_MAX
;
209 native_number
>>= CHAR_BIT
;
215 # define end_htobe16(x) end_bswap16(x)
216 # define end_be16toh(x) end_bswap16(x)
217 # define end_htobe32(x) end_bswap32(x)
218 # define end_be32toh(x) end_bswap32(x)
219 # define end_htobe64(x) end_bswap64(x)
220 # define end_be64toh(x) end_bswap64(x)
221 # define end_htole16(x) (uint16_t)(x)
222 # define end_le16toh(x) (uint16_t)(x)
223 # define end_htole32(x) (uint32_t)(x)
224 # define end_le32toh(x) (uint32_t)(x)
225 # define end_htole64(x) (uint64_t)(x)
226 # define end_le64toh(x) (uint64_t)(x)
228 # define end_htobe16(x) (uint16_t)(x)
229 # define end_be16toh(x) (uint16_t)(x)
230 # define end_htobe32(x) (uint32_t)(x)
231 # define end_be32toh(x) (uint32_t)(x)
232 # define end_htobe64(x) (uint64_t)(x)
233 # define end_be64toh(x) (uint64_t)(x)
234 # define end_htole16(x) end_bswap16(x)
235 # define end_le16toh(x) end_bswap16(x)
236 # define end_htole32(x) end_bswap32(x)
237 # define end_le32toh(x) end_bswap32(x)
238 # define end_htole64(x) end_bswap64(x)
239 # define end_le64toh(x) end_bswap64(x)
241 /* Resort to slower, but neutral code */
242 # define end_htobe16(x) end_host2net16(x)
243 # define end_be16toh(x) end_net2host16(x)
244 # define end_htobe32(x) end_host2net32(x)
245 # define end_be32toh(x) end_net2host32(x)
246 # define end_htobe64(x) end_host2net64(x)
247 # define end_be64toh(x) end_net2host64(x)
248 # define end_htole16(x) end_bswap_16(end_host2net16(x))
249 # define end_le16toh(x) end_bswap_16(end_host2net16(x))
250 # define end_htole32(x) end_bswap_32(end_host2net32(x))
251 # define end_le32toh(x) end_bswap_32(end_host2net32(x))
252 # define end_htole64(x) end_bswap_64(end_host2net64(x))
253 # define end_le64toh(x) end_bswap_64(end_host2net64(x))
256 #define end_ntoh16(x) end_be16toh(x)
257 #define end_hton16(x) end_htobe16(x)
258 #define end_ntoh32(x) end_be32toh(x)
259 #define end_hton32(x) end_htobe32(x)
260 #define end_ntoh64(x) end_be64toh(x)
261 #define end_hton64(x) end_htobe64(x)