README: document detection methodology
[rofl0r-endianness.h.git] / endianness.h
blob0207646ad8039dec2b81a153cc22a08c6b5f822d
1 #ifndef ENDIANNESS_H
2 #define ENDIANNESS_H
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
35 will be defined.
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 catches all modern GCCs (>= 4.6) and Clang (>=3.2) */
41 #if (defined __BYTE_ORDER__) && (defined __ORDER_LITTLE_ENDIAN__)
42 # ifdef ENDIANNESS_DEBUG
43 # warning "Taking endiannes from built-in __BYTE_ORDER__"
44 # endif
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
51 # endif
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(__MSP430__) || \
58 (defined(__LITTLE_ENDIAN__) && __LITTLE_ENDIAN__ == 1) || \
59 (defined(_LITTLE_ENDIAN) && _LITTLE_ENDIAN == 1) || \
60 defined(_M_ARM) || defined(_M_ARM64) || \
61 defined(_M_IX86) || defined(_M_AMD64) /* MSVC */
62 # ifdef ENDIANNESS_DEBUG
63 # warning "Detected Little Endian target CPU"
64 # endif
65 # define ENDIANNESS_LE 1
66 # define ENDIANNESS_BE 0
67 #elif defined(__MIPSEB) || defined(_MIPSEB) || defined(MIPSEB) || \
68 defined(__MICROBLAZEEB__) || defined(__ARMEB__) || \
69 (defined(__BIG_ENDIAN__) && __BIG_ENDIAN__ == 1) || \
70 (defined(_BIG_ENDIAN) && _BIG_ENDIAN == 1)
71 # ifdef ENDIANNESS_DEBUG
72 # warning "Detected Big Endian target CPU"
73 # endif
74 # define ENDIANNESS_LE 0
75 # define ENDIANNESS_BE 1
76 /* Try to get it from a header */
77 #else
78 # if defined(__linux) || defined(__HAIKU__)
79 # ifdef ENDIANNESS_DEBUG
80 # warning "Taking endiannes from endian.h"
81 # endif
82 # include <endian.h>
83 # elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || \
84 defined(__DragonFly__)
85 # ifdef ENDIANNESS_DEBUG
86 # warning "Taking endiannes from sys/endian.h"
87 # endif
88 # include <sys/endian.h>
89 # elif defined(__APPLE__)
90 # ifdef ENDIANNESS_DEBUG
91 # warning "Taking endiannes from machine/endian.h"
92 # endif
93 # include <machine/endian.h>
94 # endif
95 #endif
97 #ifndef ENDIANNESS_LE
98 # undef ENDIANNESS_BE
99 # if 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
106 # endif
107 # elif defined(BYTE_ORDER) && defined(LITTLE_ENDIAN)
108 # if BYTE_ORDER == LITTLE_ENDIAN
109 # define ENDIANNESS_LE 1
110 # define ENDIANNESS_BE 0
111 # elif BYTE_ORDER == BIG_ENDIAN
112 # define ENDIANNESS_LE 0
113 # define ENDIANNESS_BE 1
114 # endif
115 # endif
116 #endif
118 /* In case the user passed one of -DENDIANNESS_LE or BE in CPPFLAS,
119 set the second one too */
120 #if defined(ENDIANNESS_LE) && !(defined(ENDIANNESS_BE))
121 # if ENDIANNESS_LE == 0
122 # define ENDIANNESS_BE 1
123 # else
124 # define ENDIANNESS_BE 0
125 # endif
126 #elif defined(ENDIANNESS_BE) && !(defined(ENDIANNESS_LE))
127 # if ENDIANNESS_BE == 0
128 # define ENDIANNESS_LE 1
129 # else
130 # define ENDIANNESS_LE 0
131 # endif
132 #endif
134 #if !(defined(ENDIANNESS_LE)) && !(defined(ENDIANNESS_PORTABLE_CONVERSION))
135 # 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!"
136 #endif
138 #ifdef ENDIANNESS_DEBUG
139 # if ENDIANNESS_LE == 1
140 # warning "Detected Little Endian target CPU"
141 # endif
142 # if ENDIANNESS_BE == 1
143 # warning "Detected BIG Endian target CPU"
144 # endif
145 #endif
147 #include <stdint.h>
149 static __inline uint16_t end_bswap16(uint16_t __x)
151 return (__x<<8) | (__x>>8);
154 static __inline uint32_t end_bswap32(uint32_t __x)
156 return (__x>>24) | (__x>>8&0xff00) | (__x<<8&0xff0000) | (__x<<24);
159 static __inline uint64_t end_bswap64(uint64_t __x)
161 return ((end_bswap32((uint32_t)__x)+0ULL)<<32) | (end_bswap32(__x>>32));
165 static __inline uint16_t end_htobe16_impl(uint16_t n)
167 union { int i; char c; } u = { 1 };
168 return u.c ? end_bswap16(n) : n;
171 static __inline uint16_t end_htole16_impl(uint16_t n)
173 union { int i; char c; } u = { 1 };
174 return u.c ? n : end_bswap16(n);
177 static __inline uint32_t end_htobe32_impl(uint32_t n)
179 union { int i; char c; } u = { 1 };
180 return u.c ? end_bswap32(n) : n;
183 static __inline uint32_t end_htole32_impl(uint32_t n)
185 union { int i; char c; } u = { 1 };
186 return u.c ? n : end_bswap32(n);
189 static __inline uint64_t end_htobe64_impl(uint64_t n)
191 union { int i; char c; } u = { 1 };
192 return u.c ? end_bswap64(n) : n;
195 static __inline uint64_t end_htole64_impl(uint64_t n)
197 union { int i; char c; } u = { 1 };
198 return u.c ? n : end_bswap64(n);
201 #if ENDIANNESS_LE+0 == 1
202 # define end_htobe16(x) end_bswap16(x)
203 # define end_be16toh(x) end_bswap16(x)
204 # define end_htobe32(x) end_bswap32(x)
205 # define end_be32toh(x) end_bswap32(x)
206 # define end_htobe64(x) end_bswap64(x)
207 # define end_be64toh(x) end_bswap64(x)
208 # define end_htole16(x) (uint16_t)(x)
209 # define end_le16toh(x) (uint16_t)(x)
210 # define end_htole32(x) (uint32_t)(x)
211 # define end_le32toh(x) (uint32_t)(x)
212 # define end_htole64(x) (uint64_t)(x)
213 # define end_le64toh(x) (uint64_t)(x)
214 #elif ENDIANNESS_BE+0 == 1
215 # define end_htobe16(x) (uint16_t)(x)
216 # define end_be16toh(x) (uint16_t)(x)
217 # define end_htobe32(x) (uint32_t)(x)
218 # define end_be32toh(x) (uint32_t)(x)
219 # define end_htobe64(x) (uint64_t)(x)
220 # define end_be64toh(x) (uint64_t)(x)
221 # define end_htole16(x) end_bswap16(x)
222 # define end_le16toh(x) end_bswap16(x)
223 # define end_htole32(x) end_bswap32(x)
224 # define end_le32toh(x) end_bswap32(x)
225 # define end_htole64(x) end_bswap64(x)
226 # define end_le64toh(x) end_bswap64(x)
227 #else
228 /* Resort to slower, but neutral code */
229 # define end_htobe16(x) end_htobe16_impl(x)
230 # define end_be16toh(x) end_htobe16_impl(x)
231 # define end_htobe32(x) end_htobe32_impl(x)
232 # define end_be32toh(x) end_htobe32_impl(x)
233 # define end_htobe64(x) end_htobe64_impl(x)
234 # define end_be64toh(x) end_htobe64_impl(x)
235 # define end_htole16(x) end_htole16_impl(x)
236 # define end_le16toh(x) end_htole16_impl(x)
237 # define end_htole32(x) end_htole32_impl(x)
238 # define end_le32toh(x) end_htole32_impl(x)
239 # define end_htole64(x) end_htole64_impl(x)
240 # define end_le64toh(x) end_htole64_impl(x)
241 #endif
243 #define end_ntoh16(x) end_be16toh(x)
244 #define end_hton16(x) end_htobe16(x)
245 #define end_ntoh32(x) end_be32toh(x)
246 #define end_hton32(x) end_htobe32(x)
247 #define end_ntoh64(x) end_be64toh(x)
248 #define end_hton64(x) end_htobe64(x)
250 #endif