1 #ifndef _ENDIAN_H
2 #define _ENDIAN_H
3 
4 #include <features.h>
5 
6 #define __LITTLE_ENDIAN 1234
7 #define __BIG_ENDIAN 4321
8 #define __PDP_ENDIAN 3412
9 
10 #if defined(__GNUC__) && defined(__BYTE_ORDER__)
11 #define __BYTE_ORDER __BYTE_ORDER__
12 #else
13 #include <bits/endian.h>
14 #endif
15 
16 #if defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
17 
18 #define BIG_ENDIAN __BIG_ENDIAN
19 #define LITTLE_ENDIAN __LITTLE_ENDIAN
20 #define PDP_ENDIAN __PDP_ENDIAN
21 #define BYTE_ORDER __BYTE_ORDER
22 
23 #include <stdint.h>
24 
25 #pragma clang system_header
26 
__bswap16(uint16_t __x)27 static __inline uint16_t __bswap16(uint16_t __x)
28 {
29 	return __x<<8 | __x>>8;
30 }
31 
__bswap32(uint32_t __x)32 static __inline uint32_t __bswap32(uint32_t __x)
33 {
34 	return __x>>24 | __x>>8&0xff00 | __x<<8&0xff0000 | __x<<24;
35 }
36 
__bswap64(uint64_t __x)37 static __inline uint64_t __bswap64(uint64_t __x)
38 {
39 	return __bswap32((uint32_t)__x)+0ULL<<32 | __bswap32(__x>>32);
40 }
41 
42 #if __BYTE_ORDER == __LITTLE_ENDIAN
43 #define htobe16(x) __bswap16(x)
44 #define be16toh(x) __bswap16(x)
45 #define betoh16(x) __bswap16(x)
46 #define htobe32(x) __bswap32(x)
47 #define be32toh(x) __bswap32(x)
48 #define betoh32(x) __bswap32(x)
49 #define htobe64(x) __bswap64(x)
50 #define be64toh(x) __bswap64(x)
51 #define betoh64(x) __bswap64(x)
52 #define htole16(x) (uint16_t)(x)
53 #define le16toh(x) (uint16_t)(x)
54 #define letoh16(x) (uint16_t)(x)
55 #define htole32(x) (uint32_t)(x)
56 #define le32toh(x) (uint32_t)(x)
57 #define letoh32(x) (uint32_t)(x)
58 #define htole64(x) (uint64_t)(x)
59 #define le64toh(x) (uint64_t)(x)
60 #define letoh64(x) (uint64_t)(x)
61 #else
62 #define htobe16(x) (uint16_t)(x)
63 #define be16toh(x) (uint16_t)(x)
64 #define betoh16(x) (uint16_t)(x)
65 #define htobe32(x) (uint32_t)(x)
66 #define be32toh(x) (uint32_t)(x)
67 #define betoh32(x) (uint32_t)(x)
68 #define htobe64(x) (uint64_t)(x)
69 #define be64toh(x) (uint64_t)(x)
70 #define betoh64(x) (uint64_t)(x)
71 #define htole16(x) __bswap16(x)
72 #define le16toh(x) __bswap16(x)
73 #define letoh16(x) __bswap16(x)
74 #define htole32(x) __bswap32(x)
75 #define le32toh(x) __bswap32(x)
76 #define letoh32(x) __bswap32(x)
77 #define htole64(x) __bswap64(x)
78 #define le64toh(x) __bswap64(x)
79 #define letoh64(x) __bswap64(x)
80 #endif
81 
82 #endif
83 
84 #endif
85