1 /* Copyright (c) 2004 Jeff Johnston  <jjohnstn@redhat.com> */
2 #ifndef __MACHINE_ENDIAN_H__
3 #define	__MACHINE_ENDIAN_H__
4 
5 #include <sys/cdefs.h>
6 #include <sys/_types.h>
7 #include <machine/_endian.h>
8 
9 #if _BYTE_ORDER == _LITTLE_ENDIAN
10 #define	_QUAD_HIGHWORD	1
11 #define	_QUAD_LOWWORD	0
12 #else
13 #define	_QUAD_HIGHWORD	0
14 #define	_QUAD_LOWWORD	1
15 #endif
16 
17 #if __BSD_VISIBLE
18 #define	LITTLE_ENDIAN	_LITTLE_ENDIAN
19 #define	BIG_ENDIAN	_BIG_ENDIAN
20 #define	PDP_ENDIAN	_PDP_ENDIAN
21 #define	BYTE_ORDER	_BYTE_ORDER
22 #endif
23 
24 #ifdef __GNUC__
25 #define	__bswap16(_x)	__builtin_bswap16(_x)
26 #define	__bswap32(_x)	__builtin_bswap32(_x)
27 #define	__bswap64(_x)	__builtin_bswap64(_x)
28 #else /* __GNUC__ */
29 static __inline __uint16_t
__bswap16(__uint16_t _x)30 __bswap16(__uint16_t _x)
31 {
32 
33 	return ((__uint16_t)((_x >> 8) | ((_x << 8) & 0xff00)));
34 }
35 
36 static __inline __uint32_t
__bswap32(__uint32_t _x)37 __bswap32(__uint32_t _x)
38 {
39 
40 	return ((__uint32_t)((_x >> 24) | ((_x >> 8) & 0xff00) |
41 	    ((_x << 8) & 0xff0000) | ((_x << 24) & 0xff000000)));
42 }
43 
44 static __inline __uint64_t
__bswap64(__uint64_t _x)45 __bswap64(__uint64_t _x)
46 {
47 
48 	return ((__uint64_t)((_x >> 56) | ((_x >> 40) & 0xff00) |
49 	    ((_x >> 24) & 0xff0000) | ((_x >> 8) & 0xff000000) |
50 	    ((_x << 8) & ((__uint64_t)0xff << 32)) |
51 	    ((_x << 24) & ((__uint64_t)0xff << 40)) |
52 	    ((_x << 40) & ((__uint64_t)0xff << 48)) | ((_x << 56))));
53 }
54 #endif /* !__GNUC__ */
55 
56 /* endian(3) - similar to linux <endian.h> */
57 #if _BYTE_ORDER == _LITTLE_ENDIAN
58 #define htobe16(_x) __bswap16(_x)
59 #define htole16(_x) ((__uint16_t)(_x))
60 #define be16toh(_x) __bswap16(_x)
61 #define le16toh(_x) ((__uint16_t)(_x))
62 #define htobe32(_x) __bswap32(_x)
63 #define htole32(_x) ((__uint32_t)(_x))
64 #define be32toh(_x) __bswap32(_x)
65 #define le32toh(_x) ((__uint32_t)(_x))
66 #define htobe64(_x) __bswap64(_x)
67 #define htole64(_x) ((__uint64_t)(_x))
68 #define be64toh(_x) __bswap64(_x)
69 #define le64toh(_x) ((__uint64_t)(_x))
70 #else
71 #define htobe16(_x) ((__uint16_t)(_x))
72 #define htole16(_x) __bswap16(_x)
73 #define be16toh(_x) ((__uint16_t)(_x))
74 #define le16toh(_x) __bswap16(_x)
75 #define htobe32(_x) ((__uint32_t)(_x))
76 #define htole32(_x) __bswap32(_x)
77 #define be32toh(_x) ((__uint32_t)(_x))
78 #define le32toh(_x) __bswap32(_x)
79 #define htobe64(_x) ((__uint64_t)(_x))
80 #define htole64(_x) __bswap64(_x)
81 #define be64toh(_x) ((__uint64_t)(_x))
82 #define le64toh(_x) __bswap64(_x)
83 #endif
84 
85 #endif /* __MACHINE_ENDIAN_H__ */
86