1 /* $NetBSD: endian.h,v 1.11 2006/11/16 23:01:16 uwe 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.23 2006/02/04 01:07:20 uwe 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 #include <machine/bswap.h>
125
126 /*
127 * Macros for network/external number representation conversion.
128 */
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)
134
135 #define NTOHL(x) (void) (x)
136 #define NTOHS(x) (void) (x)
137 #define HTONL(x) (void) (x)
138 #define HTONS(x) (void) (x)
139
140 #else /* LITTLE_ENDIAN || !defined(__lint__) */
141
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
149
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__) */
155
156 /*
157 * Macros to convert to a specific endianness.
158 */
159
160 #if BYTE_ORDER == BIG_ENDIAN
161
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))
168
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))
175
176 #else /* LITTLE_ENDIAN */
177
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)
184
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)
191
192 #endif /* LITTLE_ENDIAN */
193
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)
200
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)
207
208 /*
209 * Routines to encode/decode big- and little-endian multi-octet values
210 * to/from an octet stream.
211 */
212
213 static __inline void __unused
be16enc(void * buf,uint16_t u)214 be16enc(void *buf, uint16_t u)
215 {
216 uint8_t *p = (uint8_t *)buf;
217
218 p[0] = ((unsigned)u >> 8) & 0xff;
219 p[1] = u & 0xff;
220 }
221
222 static __inline void __unused
le16enc(void * buf,uint16_t u)223 le16enc(void *buf, uint16_t u)
224 {
225 uint8_t *p = (uint8_t *)buf;
226
227 p[0] = u & 0xff;
228 p[1] = ((unsigned)u >> 8) & 0xff;
229 }
230
231 static __inline uint16_t __unused
be16dec(const void * buf)232 be16dec(const void *buf)
233 {
234 const uint8_t *p = (const uint8_t *)buf;
235
236 return ((p[0] << 8) | p[1]);
237 }
238
239 static __inline uint16_t __unused
le16dec(const void * buf)240 le16dec(const void *buf)
241 {
242 const uint8_t *p = (const uint8_t *)buf;
243
244 return ((p[1] << 8) | p[0]);
245 }
246
247 static __inline void __unused
be32enc(void * buf,uint32_t u)248 be32enc(void *buf, uint32_t u)
249 {
250 uint8_t *p = (uint8_t *)buf;
251
252 p[0] = (u >> 24) & 0xff;
253 p[1] = (u >> 16) & 0xff;
254 p[2] = (u >> 8) & 0xff;
255 p[3] = u & 0xff;
256 }
257
258 static __inline void __unused
le32enc(void * buf,uint32_t u)259 le32enc(void *buf, uint32_t u)
260 {
261 uint8_t *p = (uint8_t *)buf;
262
263 p[0] = u & 0xff;
264 p[1] = (u >> 8) & 0xff;
265 p[2] = (u >> 16) & 0xff;
266 p[3] = (u >> 24) & 0xff;
267 }
268
269 static __inline uint32_t __unused
be32dec(const void * buf)270 be32dec(const void *buf)
271 {
272 const uint8_t *p = (const uint8_t *)buf;
273
274 return ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
275 }
276
277 static __inline uint32_t __unused
le32dec(const void * buf)278 le32dec(const void *buf)
279 {
280 const uint8_t *p = (const uint8_t *)buf;
281
282 return ((p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0]);
283 }
284
285
286 /*
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.
289 */
290 #ifndef _WIN32
291
292 static __inline void __unused
be64enc(void * buf,uint64_t u)293 be64enc(void *buf, uint64_t u)
294 {
295 uint8_t *p = (uint8_t *)buf;
296
297 be32enc(p, (uint32_t)(u >> 32));
298 be32enc(p + 4, (uint32_t)(u & 0xffffffffULL));
299 }
300
301 static __inline void __unused
le64enc(void * buf,uint64_t u)302 le64enc(void *buf, uint64_t u)
303 {
304 uint8_t *p = (uint8_t *)buf;
305
306 le32enc(p, (uint32_t)(u & 0xffffffffULL));
307 le32enc(p + 4, (uint32_t)(u >> 32));
308 }
309
310 static __inline uint64_t __unused
be64dec(const void * buf)311 be64dec(const void *buf)
312 {
313 const uint8_t *p = (const uint8_t *)buf;
314
315 return (((uint64_t)be32dec(p) << 32) | be32dec(p + 4));
316 }
317
318 static __inline uint64_t __unused
le64dec(const void * buf)319 le64dec(const void *buf)
320 {
321 const uint8_t *p = (const uint8_t *)buf;
322
323 return (le32dec(p) | ((uint64_t)le32dec(p + 4) << 32));
324 }
325 #endif /* !_WIN32 */
326
327 #endif /* !_LOCORE */
328 #endif /* _XOPEN_SOURCE || _NETBSD_SOURCE */
329 #endif /* !_SYS_ENDIAN_H_ */
330