1/*	$OpenBSD: endian.h,v 1.17 2006/01/06 18:53:05 millert Exp $	*/
2
3/*-
4 * Copyright (c) 1997 Niklas Hallqvist.  All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27/*
28 * Generic definitions for little- and big-endian systems.  Other endianesses
29 * has to be dealt with in the specific machine/endian.h file for that port.
30 *
31 * This file is meant to be included from a little- or big-endian port's
32 * machine/endian.h after setting _BYTE_ORDER to either 1234 for little endian
33 * or 4321 for big..
34 */
35
36#ifndef _SYS_ENDIAN_H_
37#define _SYS_ENDIAN_H_
38
39#include <sys/cdefs.h>
40#include <machine/endian.h>
41
42#include <stdint.h>
43
44#define _LITTLE_ENDIAN	1234
45#define _BIG_ENDIAN	4321
46#define _PDP_ENDIAN	3412
47
48#if __BSD_VISIBLE
49#define LITTLE_ENDIAN	_LITTLE_ENDIAN
50#define BIG_ENDIAN	_BIG_ENDIAN
51#define PDP_ENDIAN	_PDP_ENDIAN
52#define BYTE_ORDER	_BYTE_ORDER
53#endif
54
55#ifdef __GNUC__
56
57#define __swap16gen(x) __statement({					\
58	__uint16_t __swap16gen_x = (x);					\
59									\
60	(__uint16_t)((__swap16gen_x & 0xff) << 8 |			\
61	    (__swap16gen_x & 0xff00) >> 8);				\
62})
63
64#define __swap32gen(x) __statement({					\
65	__uint32_t __swap32gen_x = (x);					\
66									\
67	(__uint32_t)((__swap32gen_x & 0xff) << 24 |			\
68	    (__swap32gen_x & 0xff00) << 8 |				\
69	    (__swap32gen_x & 0xff0000) >> 8 |				\
70	    (__swap32gen_x & 0xff000000) >> 24);			\
71})
72
73#define __swap64gen(x) __statement({					\
74	__uint64_t __swap64gen_x = (x);					\
75									\
76	(__uint64_t)((__swap64gen_x & 0xff) << 56 |			\
77	    (__swap64gen_x & 0xff00ULL) << 40 |				\
78	    (__swap64gen_x & 0xff0000ULL) << 24 |			\
79	    (__swap64gen_x & 0xff000000ULL) << 8 |			\
80	    (__swap64gen_x & 0xff00000000ULL) >> 8 |			\
81	    (__swap64gen_x & 0xff0000000000ULL) >> 24 |			\
82	    (__swap64gen_x & 0xff000000000000ULL) >> 40 |		\
83	    (__swap64gen_x & 0xff00000000000000ULL) >> 56);		\
84})
85
86#else /* __GNUC__ */
87
88/* Note that these macros evaluate their arguments several times.  */
89#define __swap16gen(x)							\
90    (__uint16_t)(((__uint16_t)(x) & 0xff) << 8 | ((__uint16_t)(x) & 0xff00) >> 8)
91
92#define __swap32gen(x)							\
93    (__uint32_t)(((__uint32_t)(x) & 0xff) << 24 |			\
94    ((__uint32_t)(x) & 0xff00) << 8 | ((__uint32_t)(x) & 0xff0000) >> 8 |\
95    ((__uint32_t)(x) & 0xff000000) >> 24)
96
97#define __swap64gen(x)							\
98	(__uint64_t)((((__uint64_t)(x) & 0xff) << 56) |			\
99	    ((__uint64_t)(x) & 0xff00ULL) << 40 |			\
100	    ((__uint64_t)(x) & 0xff0000ULL) << 24 |			\
101	    ((__uint64_t)(x) & 0xff000000ULL) << 8 |			\
102	    ((__uint64_t)(x) & 0xff00000000ULL) >> 8 |			\
103	    ((__uint64_t)(x) & 0xff0000000000ULL) >> 24 |		\
104	    ((__uint64_t)(x) & 0xff000000000000ULL) >> 40 |		\
105	    ((__uint64_t)(x) & 0xff00000000000000ULL) >> 56)
106
107#endif /* __GNUC__ */
108
109/*
110 * Define MD_SWAP if you provide swap{16,32}md functions/macros that are
111 * optimized for your architecture,  These will be used for swap{16,32}
112 * unless the argument is a constant and we are using GCC, where we can
113 * take advantage of the CSE phase much better by using the generic version.
114 */
115#ifdef MD_SWAP
116#if __GNUC__
117
118#define __swap16(x) __statement({					\
119	__uint16_t __swap16_x = (x);					\
120									\
121	__builtin_constant_p(x) ? __swap16gen(__swap16_x) :		\
122	    __swap16md(__swap16_x);					\
123})
124
125#define __swap32(x) __statement({					\
126	__uint32_t __swap32_x = (x);					\
127									\
128	__builtin_constant_p(x) ? __swap32gen(__swap32_x) :		\
129	    __swap32md(__swap32_x);					\
130})
131
132#define __swap64(x) __statement({					\
133	__uint64_t __swap64_x = (x);					\
134									\
135	__builtin_constant_p(x) ? __swap64gen(__swap64_x) :		\
136	    __swap64md(__swap64_x);					\
137})
138
139#endif /* __GNUC__  */
140
141#else /* MD_SWAP */
142#define __swap16 __swap16gen
143#define __swap32 __swap32gen
144#define __swap64 __swap64gen
145#endif /* MD_SWAP */
146
147#define __swap16_multi(v, n) do {						\
148	__size_t __swap16_multi_n = (n);				\
149	__uint16_t *__swap16_multi_v = (v);				\
150									\
151	while (__swap16_multi_n) {					\
152		*__swap16_multi_v = swap16(*__swap16_multi_v);		\
153		__swap16_multi_v++;					\
154		__swap16_multi_n--;					\
155	}								\
156} while (0)
157
158#if __BSD_VISIBLE
159#define swap16 __swap16
160#define swap32 __swap32
161#define swap64 __swap64
162#define swap16_multi __swap16_multi
163#endif /* __BSD_VISIBLE */
164
165#if _BYTE_ORDER == _LITTLE_ENDIAN
166
167/* Can be overridden by machine/endian.h before inclusion of this file.  */
168#ifndef _QUAD_HIGHWORD
169#define _QUAD_HIGHWORD 1
170#endif
171#ifndef _QUAD_LOWWORD
172#define _QUAD_LOWWORD 0
173#endif
174
175#if __BSD_VISIBLE
176#define htobe16 __swap16
177#define htobe32 __swap32
178#define htobe64 __swap64
179#define betoh16 __swap16
180#define betoh32 __swap32
181#define betoh64 __swap64
182
183#define htole16(x) (x)
184#define htole32(x) (x)
185#define htole64(x) (x)
186#define letoh16(x) (x)
187#define letoh32(x) (x)
188#define letoh64(x) (x)
189#endif /* __BSD_VISIBLE */
190
191/* glibc compatibility. */
192__BEGIN_DECLS
193uint32_t htonl(uint32_t) __pure2;
194uint16_t htons(uint16_t) __pure2;
195uint32_t ntohl(uint32_t) __pure2;
196uint16_t ntohs(uint16_t) __pure2;
197__END_DECLS
198
199#define htonl(x) __swap32(x)
200#define htons(x) __swap16(x)
201#define ntohl(x) __swap32(x)
202#define ntohs(x) __swap16(x)
203
204/* Bionic additions */
205#define htonq(x) __swap64(x)
206#define ntohq(x) __swap64(x)
207
208#define __LITTLE_ENDIAN_BITFIELD
209
210#endif /* _BYTE_ORDER */
211
212#if _BYTE_ORDER == _BIG_ENDIAN
213
214/* Can be overridden by machine/endian.h before inclusion of this file.  */
215#ifndef _QUAD_HIGHWORD
216#define _QUAD_HIGHWORD 0
217#endif
218#ifndef _QUAD_LOWWORD
219#define _QUAD_LOWWORD 1
220#endif
221
222#if __BSD_VISIBLE
223#define htole16 __swap16
224#define htole32 __swap32
225#define htole64 __swap64
226#define letoh16 __swap16
227#define letoh32 __swap32
228#define letoh64 __swap64
229
230#define htobe16(x) (x)
231#define htobe32(x) (x)
232#define htobe64(x) (x)
233#define betoh16(x) (x)
234#define betoh32(x) (x)
235#define betoh64(x) (x)
236#endif /* __BSD_VISIBLE */
237
238#define htons(x) (x)
239#define htonl(x) (x)
240#define ntohs(x) (x)
241#define ntohl(x) (x)
242
243/* Bionic additions */
244#define ntohq(x) (x)
245#define htonq(x) (x)
246
247#define __BIG_ENDIAN_BITFIELD
248
249#endif /* _BYTE_ORDER */
250
251#if __BSD_VISIBLE
252#define	NTOHL(x) (x) = ntohl((u_int32_t)(x))
253#define	NTOHS(x) (x) = ntohs((u_int16_t)(x))
254#define	HTONL(x) (x) = htonl((u_int32_t)(x))
255#define	HTONS(x) (x) = htons((u_int16_t)(x))
256#endif
257
258
259#define  __BYTE_ORDER       _BYTE_ORDER
260#ifndef  __LITTLE_ENDIAN
261#define  __LITTLE_ENDIAN    _LITTLE_ENDIAN
262#endif
263#ifndef  __BIG_ENDIAN
264#define  __BIG_ENDIAN       _BIG_ENDIAN
265#endif
266
267
268#ifdef __BSD_VISIBLE
269/*
270 * glibc-compatible beXXtoh/leXXtoh synonyms for htobeXX/htoleXX.
271 * The BSDs export both sets of names, bionic historically only
272 * exported the ones above (or on the rhs here), and glibc only
273 * exports these names (on the lhs).
274 */
275#define be16toh(x) htobe16(x)
276#define be32toh(x) htobe32(x)
277#define be64toh(x) htobe64(x)
278#define le16toh(x) htole16(x)
279#define le32toh(x) htole32(x)
280#define le64toh(x) htole64(x)
281#endif
282
283#endif /* _SYS_ENDIAN_H_ */
284