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