4 * Created on: 29.11.2010
8 * License: Public Domain
18 #include <machine/endian.h>
21 #if (!defined(__BYTE_ORDER)) && (!defined(BYTE_ORDER))
22 #error "no __BYTE_ORDER macro found!"
26 #define __BYTE_ORDER BYTE_ORDER
29 #if (!defined(__LITTLE_ENDIAN)) && (!defined(LITTLE_ENDIAN))
30 #error "no __LITTLE_ENDIAN macro found!"
33 #ifndef __LITTLE_ENDIAN
34 #define __LITTLE_ENDIAN LITTLE_ENDIAN
35 #define __BIG_ENDIAN BIG_ENDIAN
39 #if __BYTE_ORDER == __LITTLE_ENDIAN
40 #define IS_LITTLE_ENDIAN
43 #define byteswap16(x) (((x) & 0x00ff) << 8 | ((x) & 0xff00) >> 8)
45 #define byteswap32(x) \
46 ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \
47 (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24))
50 static inline uint64_t byteswap64(uint64_t x
) {
56 r
.l
[0] = byteswap32 (w
.l
[1]);
57 r
.l
[1] = byteswap32 (w
.l
[0]);
61 #ifdef IS_LITTLE_ENDIAN
64 #define be32(X) byteswap32(X)
65 #define be16(X) byteswap16(X)
66 #define end_htole32(X) (X)
67 #define end_htole16(X) (X)
69 #define le32(X) byteswap32(X)
70 #define le16(X) byteswap16(X)
73 #define end_htole32(X) byteswap32(X)
74 #define end_htole16(X) byteswap16(X)
77 #endif /* ENDIANNESS_H_ */