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