1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36 #ifndef _SYS_ENDIAN_H_
37 #define _SYS_ENDIAN_H_
38
39 #include <sys/cdefs.h>
40
41 #define _LITTLE_ENDIAN 1234
42 #define _BIG_ENDIAN 4321
43 #define _PDP_ENDIAN 3412
44
45 #if __BSD_VISIBLE
46 #define LITTLE_ENDIAN _LITTLE_ENDIAN
47 #define BIG_ENDIAN _BIG_ENDIAN
48 #define PDP_ENDIAN _PDP_ENDIAN
49 #define BYTE_ORDER _BYTE_ORDER
50 #endif
51
52 #ifdef __GNUC__
53
54 #define __swap16gen(x) __statement({ \
55 __uint16_t __swap16gen_x = (x); \
56 \
57 (__uint16_t)((__swap16gen_x & 0xff) << 8 | \
58 (__swap16gen_x & 0xff00) >> 8); \
59 })
60
61 #define __swap32gen(x) __statement({ \
62 __uint32_t __swap32gen_x = (x); \
63 \
64 (__uint32_t)((__swap32gen_x & 0xff) << 24 | \
65 (__swap32gen_x & 0xff00) << 8 | \
66 (__swap32gen_x & 0xff0000) >> 8 | \
67 (__swap32gen_x & 0xff000000) >> 24); \
68 })
69
70 #define __swap64gen(x) __statement({ \
71 __uint64_t __swap64gen_x = (x); \
72 \
73 (__uint64_t)((__swap64gen_x & 0xff) << 56 | \
74 (__swap64gen_x & 0xff00ULL) << 40 | \
75 (__swap64gen_x & 0xff0000ULL) << 24 | \
76 (__swap64gen_x & 0xff000000ULL) << 8 | \
77 (__swap64gen_x & 0xff00000000ULL) >> 8 | \
78 (__swap64gen_x & 0xff0000000000ULL) >> 24 | \
79 (__swap64gen_x & 0xff000000000000ULL) >> 40 | \
80 (__swap64gen_x & 0xff00000000000000ULL) >> 56); \
81 })
82
83 #else
84
85
86 #define __swap16gen(x) \
87 (__uint16_t)(((__uint16_t)(x) & 0xffU) << 8 | ((__uint16_t)(x) & 0xff00U) >> 8)
88
89 #define __swap32gen(x) \
90 (__uint32_t)(((__uint32_t)(x) & 0xff) << 24 | \
91 ((__uint32_t)(x) & 0xff00) << 8 | ((__uint32_t)(x) & 0xff0000) >> 8 |\
92 ((__uint32_t)(x) & 0xff000000) >> 24)
93
94 #define __swap64gen(x) \
95 (__uint64_t)((((__uint64_t)(x) & 0xff) << 56) | \
96 ((__uint64_t)(x) & 0xff00ULL) << 40 | \
97 ((__uint64_t)(x) & 0xff0000ULL) << 24 | \
98 ((__uint64_t)(x) & 0xff000000ULL) << 8 | \
99 ((__uint64_t)(x) & 0xff00000000ULL) >> 8 | \
100 ((__uint64_t)(x) & 0xff0000000000ULL) >> 24 | \
101 ((__uint64_t)(x) & 0xff000000000000ULL) >> 40 | \
102 ((__uint64_t)(x) & 0xff00000000000000ULL) >> 56)
103
104 #endif
105
106
107
108
109
110
111
112 #ifdef MD_SWAP
113 #if __GNUC__
114
115 #define __swap16(x) __statement({ \
116 __uint16_t __swap16_x = (x); \
117 \
118 __builtin_constant_p(x) ? __swap16gen(__swap16_x) : \
119 __swap16md(__swap16_x); \
120 })
121
122 #define __swap32(x) __statement({ \
123 __uint32_t __swap32_x = (x); \
124 \
125 __builtin_constant_p(x) ? __swap32gen(__swap32_x) : \
126 __swap32md(__swap32_x); \
127 })
128
129 #define __swap64(x) __statement({ \
130 __uint64_t __swap64_x = (x); \
131 \
132 __builtin_constant_p(x) ? __swap64gen(__swap64_x) : \
133 __swap64md(__swap64_x); \
134 })
135
136 #endif
137
138 #else
139 #define __swap16 __swap16gen
140 #define __swap32 __swap32gen
141 #define __swap64 __swap64gen
142 #endif
143
144 #define __swap16_multi(v, n) do { \
145 __size_t __swap16_multi_n = (n); \
146 __uint16_t *__swap16_multi_v = (v); \
147 \
148 while (__swap16_multi_n) { \
149 *__swap16_multi_v = swap16(*__swap16_multi_v); \
150 __swap16_multi_v++; \
151 __swap16_multi_n--; \
152 } \
153 } while (0)
154
155 #if __BSD_VISIBLE
156 #define swap16 __swap16
157 #define swap32 __swap32
158 #define swap64 __swap64
159 #define swap16_multi __swap16_multi
160
161 __BEGIN_DECLS
162 __uint64_t htobe64(__uint64_t);
163 __uint32_t htobe32(__uint32_t);
164 __uint16_t htobe16(__uint16_t);
165 __uint64_t betoh64(__uint64_t);
166 __uint32_t betoh32(__uint32_t);
167 __uint16_t betoh16(__uint16_t);
168
169 __uint64_t htole64(__uint64_t);
170 __uint32_t htole32(__uint32_t);
171 __uint16_t htole16(__uint16_t);
172 __uint64_t letoh64(__uint64_t);
173 __uint32_t letoh32(__uint32_t);
174 __uint16_t letoh16(__uint16_t);
175 __END_DECLS
176 #endif
177
178 #if _BYTE_ORDER == _LITTLE_ENDIAN
179
180
181 #ifndef _QUAD_HIGHWORD
182 #define _QUAD_HIGHWORD 1
183 #endif
184 #ifndef _QUAD_LOWWORD
185 #define _QUAD_LOWWORD 0
186 #endif
187
188 #if __BSD_VISIBLE
189 #define htobe16 __swap16
190 #define htobe32 __swap32
191 #define htobe64 __swap64
192 #define betoh16 __swap16
193 #define betoh32 __swap32
194 #define betoh64 __swap64
195
196 #define htole16(x) (x)
197 #define htole32(x) (x)
198 #define htole64(x) (x)
199 #define letoh16(x) (x)
200 #define letoh32(x) (x)
201 #define letoh64(x) (x)
202 #endif
203
204 #define htons(x) __swap16(x)
205 #define htonl(x) __swap32(x)
206 #define ntohs(x) __swap16(x)
207 #define ntohl(x) __swap32(x)
208
209 #endif
210
211 #if _BYTE_ORDER == _BIG_ENDIAN
212
213
214 #ifndef _QUAD_HIGHWORD
215 #define _QUAD_HIGHWORD 0
216 #endif
217 #ifndef _QUAD_LOWWORD
218 #define _QUAD_LOWWORD 1
219 #endif
220
221 #if __BSD_VISIBLE
222 #define htole16 __swap16
223 #define htole32 __swap32
224 #define htole64 __swap64
225 #define letoh16 __swap16
226 #define letoh32 __swap32
227 #define letoh64 __swap64
228
229 #define htobe16(x) (x)
230 #define htobe32(x) (x)
231 #define htobe64(x) (x)
232 #define betoh16(x) (x)
233 #define betoh32(x) (x)
234 #define betoh64(x) (x)
235 #endif
236
237 #define htons(x) (x)
238 #define htonl(x) (x)
239 #define ntohs(x) (x)
240 #define ntohl(x) (x)
241
242 #endif
243
244 #if __BSD_VISIBLE
245 #define NTOHL(x) (x) = ntohl((u_int32_t)(x))
246 #define NTOHS(x) (x) = ntohs((u_int16_t)(x))
247 #define HTONL(x) (x) = htonl((u_int32_t)(x))
248 #define HTONS(x) (x) = htons((u_int16_t)(x))
249 #endif
250
251 #endif