FreeBSD/Linux Kernel Cross Reference
sys/sys/endian.h
1 /* $NetBSD: endian.h,v 1.24 2006/05/05 15:08:11 christos Exp $ */
2
3 /*
4 * Copyright (c) 1987, 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 * @(#)endian.h 8.1 (Berkeley) 6/11/93
32 */
33
34 #ifndef _SYS_ENDIAN_H_
35 #define _SYS_ENDIAN_H_
36
37 #include <sys/featuretest.h>
38
39 /*
40 * Definitions for byte order, according to byte significance from low
41 * address to high.
42 */
43 #define _LITTLE_ENDIAN 1234 /* LSB first: i386, vax */
44 #define _BIG_ENDIAN 4321 /* MSB first: 68000, ibm, net */
45 #define _PDP_ENDIAN 3412 /* LSB first in word, MSW first in long */
46
47
48 #if defined(_XOPEN_SOURCE) || defined(_NETBSD_SOURCE)
49 #ifndef _LOCORE
50
51 /* C-family endian-ness definitions */
52
53 #include <sys/ansi.h>
54 #include <sys/cdefs.h>
55 #include <sys/types.h>
56
57 #ifndef in_addr_t
58 typedef __in_addr_t in_addr_t;
59 #define in_addr_t __in_addr_t
60 #endif
61
62 #ifndef in_port_t
63 typedef __in_port_t in_port_t;
64 #define in_port_t __in_port_t
65 #endif
66
67 __BEGIN_DECLS
68 uint32_t htonl(uint32_t) __attribute__((__const__));
69 uint16_t htons(uint16_t) __attribute__((__const__));
70 uint32_t ntohl(uint32_t) __attribute__((__const__));
71 uint16_t ntohs(uint16_t) __attribute__((__const__));
72 __END_DECLS
73
74 #endif /* !_LOCORE */
75 #endif /* _XOPEN_SOURCE || _NETBSD_SOURCE */
76
77
78 #include <machine/endian_machdep.h>
79
80 /*
81 * Define the order of 32-bit words in 64-bit words.
82 */
83 #if _BYTE_ORDER == _LITTLE_ENDIAN
84 #define _QUAD_HIGHWORD 1
85 #define _QUAD_LOWWORD 0
86 #endif
87
88 #if _BYTE_ORDER == _BIG_ENDIAN
89 #define _QUAD_HIGHWORD 0
90 #define _QUAD_LOWWORD 1
91 #endif
92
93
94 #if defined(_XOPEN_SOURCE) || defined(_NETBSD_SOURCE)
95 /*
96 * Traditional names for byteorder. These are defined as the numeric
97 * sequences so that third party code can "#define XXX_ENDIAN" and not
98 * cause errors.
99 */
100 #define LITTLE_ENDIAN 1234 /* LSB first: i386, vax */
101 #define BIG_ENDIAN 4321 /* MSB first: 68000, ibm, net */
102 #define PDP_ENDIAN 3412 /* LSB first in word, MSW first in long */
103 #define BYTE_ORDER _BYTE_ORDER
104
105 #ifndef _LOCORE
106
107 #include <machine/bswap.h>
108
109 /*
110 * Macros for network/external number representation conversion.
111 */
112 #if BYTE_ORDER == BIG_ENDIAN && !defined(__lint__)
113 #define ntohl(x) (x)
114 #define ntohs(x) (x)
115 #define htonl(x) (x)
116 #define htons(x) (x)
117
118 #define NTOHL(x) (void) (x)
119 #define NTOHS(x) (void) (x)
120 #define HTONL(x) (void) (x)
121 #define HTONS(x) (void) (x)
122
123 #else /* LITTLE_ENDIAN || !defined(__lint__) */
124
125 #define ntohl(x) bswap32((uint32_t)(x))
126 #define ntohs(x) bswap16((uint16_t)(x))
127 #define htonl(x) bswap32((uint32_t)(x))
128 #define htons(x) bswap16((uint16_t)(x))
129
130 #define NTOHL(x) (x) = ntohl((uint32_t)(x))
131 #define NTOHS(x) (x) = ntohs((uint16_t)(x))
132 #define HTONL(x) (x) = htonl((uint32_t)(x))
133 #define HTONS(x) (x) = htons((uint16_t)(x))
134 #endif /* LITTLE_ENDIAN || !defined(__lint__) */
135
136 /*
137 * Macros to convert to a specific endianness.
138 */
139
140 #if BYTE_ORDER == BIG_ENDIAN
141
142 #define htobe16(x) (x)
143 #define htobe32(x) (x)
144 #define htobe64(x) (x)
145 #define htole16(x) bswap16((uint16_t)(x))
146 #define htole32(x) bswap32((uint32_t)(x))
147 #define htole64(x) bswap64((uint64_t)(x))
148
149 #define HTOBE16(x) (void) (x)
150 #define HTOBE32(x) (void) (x)
151 #define HTOBE64(x) (void) (x)
152 #define HTOLE16(x) (x) = bswap16((uint16_t)(x))
153 #define HTOLE32(x) (x) = bswap32((uint32_t)(x))
154 #define HTOLE64(x) (x) = bswap64((uint64_t)(x))
155
156 #else /* LITTLE_ENDIAN */
157
158 #define htobe16(x) bswap16((uint16_t)(x))
159 #define htobe32(x) bswap32((uint32_t)(x))
160 #define htobe64(x) bswap64((uint64_t)(x))
161 #define htole16(x) (x)
162 #define htole32(x) (x)
163 #define htole64(x) (x)
164
165 #define HTOBE16(x) (x) = bswap16((uint16_t)(x))
166 #define HTOBE32(x) (x) = bswap32((uint32_t)(x))
167 #define HTOBE64(x) (x) = bswap64((uint64_t)(x))
168 #define HTOLE16(x) (void) (x)
169 #define HTOLE32(x) (void) (x)
170 #define HTOLE64(x) (void) (x)
171
172 #endif /* LITTLE_ENDIAN */
173
174 #define be16toh(x) htobe16(x)
175 #define be32toh(x) htobe32(x)
176 #define be64toh(x) htobe64(x)
177 #define le16toh(x) htole16(x)
178 #define le32toh(x) htole32(x)
179 #define le64toh(x) htole64(x)
180
181 #define BE16TOH(x) HTOBE16(x)
182 #define BE32TOH(x) HTOBE32(x)
183 #define BE64TOH(x) HTOBE64(x)
184 #define LE16TOH(x) HTOLE16(x)
185 #define LE32TOH(x) HTOLE32(x)
186 #define LE64TOH(x) HTOLE64(x)
187
188 /*
189 * Routines to encode/decode big- and little-endian multi-octet values
190 * to/from an octet stream.
191 */
192
193 static __inline void __unused
194 be16enc(void *buf, uint16_t u)
195 {
196 uint8_t *p = (uint8_t *)buf;
197
198 p[0] = ((unsigned)u >> 8) & 0xff;
199 p[1] = u & 0xff;
200 }
201
202 static __inline void __unused
203 le16enc(void *buf, uint16_t u)
204 {
205 uint8_t *p = (uint8_t *)buf;
206
207 p[0] = u & 0xff;
208 p[1] = ((unsigned)u >> 8) & 0xff;
209 }
210
211 static __inline uint16_t __unused
212 be16dec(const void *buf)
213 {
214 const uint8_t *p = (const uint8_t *)buf;
215
216 return ((p[0] << 8) | p[1]);
217 }
218
219 static __inline uint16_t __unused
220 le16dec(const void *buf)
221 {
222 const uint8_t *p = (const uint8_t *)buf;
223
224 return ((p[1] << 8) | p[0]);
225 }
226
227 static __inline void __unused
228 be32enc(void *buf, uint32_t u)
229 {
230 uint8_t *p = (uint8_t *)buf;
231
232 p[0] = (u >> 24) & 0xff;
233 p[1] = (u >> 16) & 0xff;
234 p[2] = (u >> 8) & 0xff;
235 p[3] = u & 0xff;
236 }
237
238 static __inline void __unused
239 le32enc(void *buf, uint32_t u)
240 {
241 uint8_t *p = (uint8_t *)buf;
242
243 p[0] = u & 0xff;
244 p[1] = (u >> 8) & 0xff;
245 p[2] = (u >> 16) & 0xff;
246 p[3] = (u >> 24) & 0xff;
247 }
248
249 static __inline uint32_t __unused
250 be32dec(const void *buf)
251 {
252 const uint8_t *p = (const uint8_t *)buf;
253
254 return ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
255 }
256
257 static __inline uint32_t __unused
258 le32dec(const void *buf)
259 {
260 const uint8_t *p = (const uint8_t *)buf;
261
262 return ((p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0]);
263 }
264
265 static __inline void __unused
266 be64enc(void *buf, uint64_t u)
267 {
268 uint8_t *p = (uint8_t *)buf;
269
270 be32enc(p, (uint32_t)(u >> 32));
271 be32enc(p + 4, (uint32_t)(u & 0xffffffffULL));
272 }
273
274 static __inline void __unused
275 le64enc(void *buf, uint64_t u)
276 {
277 uint8_t *p = (uint8_t *)buf;
278
279 le32enc(p, (uint32_t)(u & 0xffffffffULL));
280 le32enc(p + 4, (uint32_t)(u >> 32));
281 }
282
283 static __inline uint64_t __unused
284 be64dec(const void *buf)
285 {
286 const uint8_t *p = (const uint8_t *)buf;
287
288 return (((uint64_t)be32dec(p) << 32) | be32dec(p + 4));
289 }
290
291 static __inline uint64_t __unused
292 le64dec(const void *buf)
293 {
294 const uint8_t *p = (const uint8_t *)buf;
295
296 return (le32dec(p) | ((uint64_t)le32dec(p + 4) << 32));
297 }
298
299 #endif /* !_LOCORE */
300 #endif /* _XOPEN_SOURCE || _NETBSD_SOURCE */
301 #endif /* !_SYS_ENDIAN_H_ */
Cache object: c0d87208a632f113888ca837d07d8f51
|