xref: /netbsd-src/sys/arch/hpc/stand/include/machine/endian.h (revision e5548b402ae4c44fb816de42c7bba9581ce23ef5)
1 /*	$NetBSD: endian.h,v 1.3 2005/12/11 12:17:29 christos Exp $	*/
2 
3 /* Windows CE architecture */
4 
5 /*
6  * This file should be just:
7 
8 #include <sys/endian.h>
9 
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.
13  *
14  * Instead of polluting sys/endian.h with WinCE compatibility
15  * ugliness, pull a copy here, so that we can hack it privately.
16  */
17 
18 /*	From: NetBSD: endian.h,v 1.15 2005/02/03 19:16:10 perry Exp	*/
19 
20 /*
21  * Copyright (c) 1987, 1991, 1993
22  *	The Regents of the University of California.  All rights reserved.
23  *
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.
35  *
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.
47  *
48  *	@(#)endian.h	8.1 (Berkeley) 6/11/93
49  */
50 
51 #ifndef _SYS_ENDIAN_H_
52 #define _SYS_ENDIAN_H_
53 
54 #include <sys/featuretest.h>
55 
56 /*
57  * Definitions for byte order, according to byte significance from low
58  * address to high.
59  */
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 */
63 
64 
65 #if defined(_XOPEN_SOURCE) || defined(_NETBSD_SOURCE)
66 #ifndef _LOCORE
67 
68 /* C-family endian-ness definitions */
69 
70 #include <sys/ansi.h>
71 #include <sys/cdefs.h>
72 #include <sys/types.h>
73 
74 #ifndef in_addr_t
75 typedef __in_addr_t	in_addr_t;
76 #define	in_addr_t	__in_addr_t
77 #endif
78 
79 #ifndef in_port_t
80 typedef __in_port_t	in_port_t;
81 #define	in_port_t	__in_port_t
82 #endif
83 
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
90 
91 #endif /* !_LOCORE */
92 #endif /* _XOPEN_SOURCE || _NETBSD_SOURCE */
93 
94 
95 #include <machine/endian_machdep.h>
96 
97 /*
98  * Define the order of 32-bit words in 64-bit words.
99  */
100 #if _BYTE_ORDER == _LITTLE_ENDIAN
101 #define _QUAD_HIGHWORD 1
102 #define _QUAD_LOWWORD 0
103 #endif
104 
105 #if _BYTE_ORDER == _BIG_ENDIAN
106 #define _QUAD_HIGHWORD 0
107 #define _QUAD_LOWWORD 1
108 #endif
109 
110 
111 #if defined(_XOPEN_SOURCE) || defined(_NETBSD_SOURCE)
112 /*
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.
116  */
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
121 
122 #ifndef _LOCORE
123 
124 /*
125  * Macros for network/external number representation conversion.
126  */
127 #if BYTE_ORDER == BIG_ENDIAN && !defined(__lint__)
128 #define	ntohl(x)	(x)
129 #define	ntohs(x)	(x)
130 #define	htonl(x)	(x)
131 #define	htons(x)	(x)
132 
133 #define	NTOHL(x)	(void) (x)
134 #define	NTOHS(x)	(void) (x)
135 #define	HTONL(x)	(void) (x)
136 #define	HTONS(x)	(void) (x)
137 
138 #else	/* LITTLE_ENDIAN || !defined(__lint__) */
139 
140 #define	NTOHL(x)	(x) = ntohl((uint32_t)(x))
141 #define	NTOHS(x)	(x) = ntohs((uint16_t)(x))
142 #define	HTONL(x)	(x) = htonl((uint32_t)(x))
143 #define	HTONS(x)	(x) = htons((uint16_t)(x))
144 #endif	/* LITTLE_ENDIAN || !defined(__lint__) */
145 
146 /*
147  * Macros to convert to a specific endianness.
148  */
149 
150 #include <machine/bswap.h>
151 
152 #if BYTE_ORDER == BIG_ENDIAN
153 
154 #define htobe16(x)	(x)
155 #define htobe32(x)	(x)
156 #define htobe64(x)	(x)
157 #define htole16(x)	bswap16((u_int16_t)(x))
158 #define htole32(x)	bswap32((u_int32_t)(x))
159 #define htole64(x)	bswap64((u_int64_t)(x))
160 
161 #define HTOBE16(x)	(void) (x)
162 #define HTOBE32(x)	(void) (x)
163 #define HTOBE64(x)	(void) (x)
164 #define HTOLE16(x)	(x) = bswap16((u_int16_t)(x))
165 #define HTOLE32(x)	(x) = bswap32((u_int32_t)(x))
166 #define HTOLE64(x)	(x) = bswap64((u_int64_t)(x))
167 
168 #else	/* LITTLE_ENDIAN */
169 
170 #define htobe16(x)	bswap16((u_int16_t)(x))
171 #define htobe32(x)	bswap32((u_int32_t)(x))
172 #define htobe64(x)	bswap64((u_int64_t)(x))
173 #define htole16(x)	(x)
174 #define htole32(x)	(x)
175 #define htole64(x)	(x)
176 
177 #define HTOBE16(x)	(x) = bswap16((u_int16_t)(x))
178 #define HTOBE32(x)	(x) = bswap32((u_int32_t)(x))
179 #define HTOBE64(x)	(x) = bswap64((u_int64_t)(x))
180 #define HTOLE16(x)	(void) (x)
181 #define HTOLE32(x)	(void) (x)
182 #define HTOLE64(x)	(void) (x)
183 
184 #endif	/* LITTLE_ENDIAN */
185 
186 #define be16toh(x)	htobe16(x)
187 #define be32toh(x)	htobe32(x)
188 #define be64toh(x)	htobe64(x)
189 #define le16toh(x)	htole16(x)
190 #define le32toh(x)	htole32(x)
191 #define le64toh(x)	htole64(x)
192 
193 #define BE16TOH(x)	HTOBE16(x)
194 #define BE32TOH(x)	HTOBE32(x)
195 #define BE64TOH(x)	HTOBE64(x)
196 #define LE16TOH(x)	HTOLE16(x)
197 #define LE32TOH(x)	HTOLE32(x)
198 #define LE64TOH(x)	HTOLE64(x)
199 
200 /*
201  * Routines to encode/decode big- and little-endian multi-octet values
202  * to/from an octet stream.
203  */
204 
205 static __inline void __unused
206 be16enc(void *buf, uint16_t u)
207 {
208 	uint8_t *p = (uint8_t *)buf;
209 
210 	p[0] = ((unsigned)u >> 8) & 0xff;
211 	p[1] = u & 0xff;
212 }
213 
214 static __inline void __unused
215 le16enc(void *buf, uint16_t u)
216 {
217 	uint8_t *p = (uint8_t *)buf;
218 
219 	p[0] = u & 0xff;
220 	p[1] = ((unsigned)u >> 8) & 0xff;
221 }
222 
223 static __inline uint16_t __unused
224 be16dec(const void *buf)
225 {
226 	const uint8_t *p = (const uint8_t *)buf;
227 
228 	return ((p[0] << 8) | p[1]);
229 }
230 
231 static __inline uint16_t __unused
232 le16dec(const void *buf)
233 {
234 	const uint8_t *p = (const uint8_t *)buf;
235 
236 	return ((p[1] << 8) | p[0]);
237 }
238 
239 static __inline void __unused
240 be32enc(void *buf, uint32_t u)
241 {
242 	uint8_t *p = (uint8_t *)buf;
243 
244 	p[0] = (u >> 24) & 0xff;
245 	p[1] = (u >> 16) & 0xff;
246 	p[2] = (u >> 8) & 0xff;
247 	p[3] = u & 0xff;
248 }
249 
250 static __inline void __unused
251 le32enc(void *buf, uint32_t u)
252 {
253 	uint8_t *p = (uint8_t *)buf;
254 
255 	p[0] = u & 0xff;
256 	p[1] = (u >> 8) & 0xff;
257 	p[2] = (u >> 16) & 0xff;
258 	p[3] = (u >> 24) & 0xff;
259 }
260 
261 static __inline uint32_t __unused
262 be32dec(const void *buf)
263 {
264 	const uint8_t *p = (const uint8_t *)buf;
265 
266 	return ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
267 }
268 
269 static __inline uint32_t __unused
270 le32dec(const void *buf)
271 {
272 	const uint8_t *p = (const uint8_t *)buf;
273 
274 	return ((p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0]);
275 }
276 
277 
278 /*
279  * XXX: uwe: ULL suffix makes eVC unhappy.  Since nothing in hpcboot
280  * needs 64-bit enc/dec routines - just ifdef them out for now.
281  */
282 #ifndef _WIN32
283 
284 static __inline void __unused
285 be64enc(void *buf, uint64_t u)
286 {
287 	uint8_t *p = (uint8_t *)buf;
288 
289 	be32enc(p, (uint32_t)(u >> 32));
290 	be32enc(p + 4, (uint32_t)(u & 0xffffffffULL));
291 }
292 
293 static __inline void __unused
294 le64enc(void *buf, uint64_t u)
295 {
296 	uint8_t *p = (uint8_t *)buf;
297 
298 	le32enc(p, (uint32_t)(u & 0xffffffffULL));
299 	le32enc(p + 4, (uint32_t)(u >> 32));
300 }
301 
302 static __inline uint64_t __unused
303 be64dec(const void *buf)
304 {
305 	const uint8_t *p = (const uint8_t *)buf;
306 
307 	return (((uint64_t)be32dec(p) << 32) | be32dec(p + 4));
308 }
309 
310 static __inline uint64_t __unused
311 le64dec(const void *buf)
312 {
313 	const uint8_t *p = (const uint8_t *)buf;
314 
315 	return (le32dec(p) | ((uint64_t)le32dec(p + 4) << 32));
316 }
317 #endif /* !_WIN32 */
318 
319 #endif /* !_LOCORE */
320 #endif /* _XOPEN_SOURCE || _NETBSD_SOURCE */
321 #endif /* !_SYS_ENDIAN_H_ */
322