1 | #ifndef __MACHINE_ENDIAN_H__ |
---|
2 | #define __MACHINE_ENDIAN_H__ |
---|
3 | |
---|
4 | #include <sys/cdefs.h> |
---|
5 | #include <sys/_types.h> |
---|
6 | #include <machine/_endian.h> |
---|
7 | |
---|
8 | #if _BYTE_ORDER == _LITTLE_ENDIAN |
---|
9 | #define _QUAD_HIGHWORD 1 |
---|
10 | #define _QUAD_LOWWORD 0 |
---|
11 | #else |
---|
12 | #define _QUAD_HIGHWORD 0 |
---|
13 | #define _QUAD_LOWWORD 1 |
---|
14 | #endif |
---|
15 | |
---|
16 | #if __BSD_VISIBLE |
---|
17 | #define LITTLE_ENDIAN _LITTLE_ENDIAN |
---|
18 | #define BIG_ENDIAN _BIG_ENDIAN |
---|
19 | #define PDP_ENDIAN _PDP_ENDIAN |
---|
20 | #define BYTE_ORDER _BYTE_ORDER |
---|
21 | #endif |
---|
22 | |
---|
23 | #ifdef __GNUC__ |
---|
24 | #define __bswap16(_x) __builtin_bswap16(_x) |
---|
25 | #define __bswap32(_x) __builtin_bswap32(_x) |
---|
26 | #define __bswap64(_x) __builtin_bswap64(_x) |
---|
27 | #else /* __GNUC__ */ |
---|
28 | static __inline __uint16_t |
---|
29 | __bswap16(__uint16_t _x) |
---|
30 | { |
---|
31 | |
---|
32 | return ((__uint16_t)((_x >> 8) | ((_x << 8) & 0xff00))); |
---|
33 | } |
---|
34 | |
---|
35 | static __inline __uint32_t |
---|
36 | __bswap32(__uint32_t _x) |
---|
37 | { |
---|
38 | |
---|
39 | return ((__uint32_t)((_x >> 24) | ((_x >> 8) & 0xff00) | |
---|
40 | ((_x << 8) & 0xff0000) | ((_x << 24) & 0xff000000))); |
---|
41 | } |
---|
42 | |
---|
43 | static __inline __uint64_t |
---|
44 | __bswap64(__uint64_t _x) |
---|
45 | { |
---|
46 | |
---|
47 | return ((__uint64_t)((_x >> 56) | ((_x >> 40) & 0xff00) | |
---|
48 | ((_x >> 24) & 0xff0000) | ((_x >> 8) & 0xff000000) | |
---|
49 | ((_x << 8) & ((__uint64_t)0xff << 32)) | |
---|
50 | ((_x << 24) & ((__uint64_t)0xff << 40)) | |
---|
51 | ((_x << 40) & ((__uint64_t)0xff << 48)) | ((_x << 56)))); |
---|
52 | } |
---|
53 | #endif /* !__GNUC__ */ |
---|
54 | |
---|
55 | #ifndef __machine_host_to_from_network_defined |
---|
56 | #if _BYTE_ORDER == _LITTLE_ENDIAN |
---|
57 | #define __htonl(_x) __bswap32(_x) |
---|
58 | #define __htons(_x) __bswap16(_x) |
---|
59 | #define __ntohl(_x) __bswap32(_x) |
---|
60 | #define __ntohs(_x) __bswap16(_x) |
---|
61 | #else |
---|
62 | #define __htonl(_x) ((__uint32_t)(_x)) |
---|
63 | #define __htons(_x) ((__uint16_t)(_x)) |
---|
64 | #define __ntohl(_x) ((__uint32_t)(_x)) |
---|
65 | #define __ntohs(_x) ((__uint16_t)(_x)) |
---|
66 | #endif |
---|
67 | #endif /* __machine_host_to_from_network_defined */ |
---|
68 | |
---|
69 | #endif /* __MACHINE_ENDIAN_H__ */ |
---|