tools/include: add more endian macros
[openwrt/staging/stintel.git] / tools / include / endian.h
1 #ifndef __endian_compat_h
2 #define __endian_compat_h
3
4 #if defined(__linux__) || defined(__CYGWIN__)
5 #include <byteswap.h>
6 #include_next <endian.h>
7 #elif defined(__APPLE__)
8 #include <netinet/in.h>
9 #include <libkern/OSByteOrder.h>
10 #define bswap_16(x) OSSwapInt16(x)
11 #define bswap_32(x) OSSwapInt32(x)
12 #define bswap_64(x) OSSwapInt64(x)
13 #elif defined(__FreeBSD__)
14 #include <sys/endian.h>
15 #define bswap_16(x) bswap16(x)
16 #define bswap_32(x) bswap32(x)
17 #define bswap_64(x) bswap64(x)
18 #elif defined(__OpenBSD__)
19 #include <sys/types.h>
20 #define bswap_16(x) __swap16(x)
21 #define bswap_32(x) __swap32(x)
22 #define bswap_64(x) __swap64(x)
23 #else
24 #include <machine/endian.h>
25 #define bswap_16(x) swap16(x)
26 #define bswap_32(x) swap32(x)
27 #define bswap_64(x) swap64(x)
28 #endif
29
30 #ifndef __BYTE_ORDER
31 #define __BYTE_ORDER BYTE_ORDER
32 #endif
33 #ifndef __BIG_ENDIAN
34 #define __BIG_ENDIAN BIG_ENDIAN
35 #endif
36 #ifndef __LITTLE_ENDIAN
37 #define __LITTLE_ENDIAN LITTLE_ENDIAN
38 #endif
39
40 #ifndef __linux__
41 #if __BYTE_ORDER == __LITTLE_ENDIAN
42 #define htobe16(x) bswap_16(x)
43 #define be16toh(x) bswap_16(x)
44 #define htobe32(x) bswap_32(x)
45 #define be32toh(x) bswap_32(x)
46 #define htobe64(x) bswap_64(x)
47 #define be64toh(x) bswap_64(x)
48 #define htole16(x) (uint16_t)(x)
49 #define le16toh(x) (uint16_t)(x)
50 #define htole32(x) (uint32_t)(x)
51 #define le32toh(x) (uint32_t)(x)
52 #define htole64(x) (uint64_t)(x)
53 #define le64toh(x) (uint64_t)(x)
54 #else
55 #define htobe16(x) (uint16_t)(x)
56 #define be16toh(x) (uint16_t)(x)
57 #define htobe32(x) (uint32_t)(x)
58 #define be32toh(x) (uint32_t)(x)
59 #define htobe64(x) (uint64_t)(x)
60 #define be64toh(x) (uint64_t)(x)
61 #define htole16(x) bswap_16(x)
62 #define le16toh(x) bswap_16(x)
63 #define htole32(x) bswap_32(x)
64 #define le32toh(x) bswap_32(x)
65 #define htole64(x) bswap_64(x)
66 #define le64toh(x) bswap_64(x)
67 #endif
68
69 #if defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
70 #if __BYTE_ORDER == __LITTLE_ENDIAN
71 #define betoh16(x) bswap_16(x)
72 #define betoh32(x) bswap_32(x)
73 #define betoh64(x) bswap_64(x)
74 #define letoh16(x) (uint16_t)(x)
75 #define letoh32(x) (uint32_t)(x)
76 #define letoh64(x) (uint64_t)(x)
77 #else
78 #define betoh16(x) (uint16_t)(x)
79 #define betoh32(x) (uint32_t)(x)
80 #define betoh64(x) (uint64_t)(x)
81 #define letoh16(x) bswap_16(x)
82 #define letoh32(x) bswap_32(x)
83 #define letoh64(x) bswap_64(x)
84 #endif
85 #endif
86 #endif
87
88 #endif