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#include <machine/_types.h>
42
43#define _LITTLE_ENDIAN	1234
44#define _BIG_ENDIAN	4321
45#define _PDP_ENDIAN	3412
46
47#if __BSD_VISIBLE
48#define LITTLE_ENDIAN	_LITTLE_ENDIAN
49#define BIG_ENDIAN	_BIG_ENDIAN
50#define PDP_ENDIAN	_PDP_ENDIAN
51#define BYTE_ORDER	_BYTE_ORDER
52#endif
53
54#ifdef __GNUC__
55
56#define __swap16gen(x) __statement({					\
57	__uint16_t __swap16gen_x = (x);					\
58									\
59	(__uint16_t)((__swap16gen_x & 0xff) << 8 |			\
60	    (__swap16gen_x & 0xff00) >> 8);				\
61})
62
63#define __swap32gen(x) __statement({					\
64	__uint32_t __swap32gen_x = (x);					\
65									\
66	(__uint32_t)((__swap32gen_x & 0xff) << 24 |			\
67	    (__swap32gen_x & 0xff00) << 8 |				\
68	    (__swap32gen_x & 0xff0000) >> 8 |				\
69	    (__swap32gen_x & 0xff000000) >> 24);			\
70})
71
72#define __swap64gen(x) __statement({					\
73	__uint64_t __swap64gen_x = (x);					\
74									\
75	(__uint64_t)((__swap64gen_x & 0xff) << 56 |			\
76	    (__swap64gen_x & 0xff00ULL) << 40 |				\
77	    (__swap64gen_x & 0xff0000ULL) << 24 |			\
78	    (__swap64gen_x & 0xff000000ULL) << 8 |			\
79	    (__swap64gen_x & 0xff00000000ULL) >> 8 |			\
80	    (__swap64gen_x & 0xff0000000000ULL) >> 24 |			\
81	    (__swap64gen_x & 0xff000000000000ULL) >> 40 |		\
82	    (__swap64gen_x & 0xff00000000000000ULL) >> 56);		\
83})
84
85#else /* __GNUC__ */
86
87/* Note that these macros evaluate their arguments several times.  */
88#define __swap16gen(x)							\
89    (__uint16_t)(((__uint16_t)(x) & 0xff) << 8 | ((__uint16_t)(x) & 0xff00) >> 8)
90
91#define __swap32gen(x)							\
92    (__uint32_t)(((__uint32_t)(x) & 0xff) << 24 |			\
93    ((__uint32_t)(x) & 0xff00) << 8 | ((__uint32_t)(x) & 0xff0000) >> 8 |\
94    ((__uint32_t)(x) & 0xff000000) >> 24)
95
96#define __swap64gen(x)							\
97	(__uint64_t)((((__uint64_t)(x) & 0xff) << 56) |			\
98	    ((__uint64_t)(x) & 0xff00ULL) << 40 |			\
99	    ((__uint64_t)(x) & 0xff0000ULL) << 24 |			\
100	    ((__uint64_t)(x) & 0xff000000ULL) << 8 |			\
101	    ((__uint64_t)(x) & 0xff00000000ULL) >> 8 |			\
102	    ((__uint64_t)(x) & 0xff0000000000ULL) >> 24 |		\
103	    ((__uint64_t)(x) & 0xff000000000000ULL) >> 40 |		\
104	    ((__uint64_t)(x) & 0xff00000000000000ULL) >> 56)
105
106#endif /* __GNUC__ */
107
108/*
109 * Define MD_SWAP if you provide swap{16,32}md functions/macros that are
110 * optimized for your architecture,  These will be used for swap{16,32}
111 * unless the argument is a constant and we are using GCC, where we can
112 * take advantage of the CSE phase much better by using the generic version.
113 */
114#ifdef MD_SWAP
115#if __GNUC__
116
117#define __swap16(x) __statement({					\
118	__uint16_t __swap16_x = (x);					\
119									\
120	__builtin_constant_p(x) ? __swap16gen(__swap16_x) :		\
121	    __swap16md(__swap16_x);					\
122})
123
124#define __swap32(x) __statement({					\
125	__uint32_t __swap32_x = (x);					\
126									\
127	__builtin_constant_p(x) ? __swap32gen(__swap32_x) :		\
128	    __swap32md(__swap32_x);					\
129})
130
131#define __swap64(x) __statement({					\
132	__uint64_t __swap64_x = (x);					\
133									\
134	__builtin_constant_p(x) ? __swap64gen(__swap64_x) :		\
135	    __swap64md(__swap64_x);					\
136})
137
138#endif /* __GNUC__  */
139
140#else /* MD_SWAP */
141#define __swap16 __swap16gen
142#define __swap32 __swap32gen
143#define __swap64 __swap64gen
144#endif /* MD_SWAP */
145
146#define __swap16_multi(v, n) do {						\
147	__size_t __swap16_multi_n = (n);				\
148	__uint16_t *__swap16_multi_v = (v);				\
149									\
150	while (__swap16_multi_n) {					\
151		*__swap16_multi_v = swap16(*__swap16_multi_v);		\
152		__swap16_multi_v++;					\
153		__swap16_multi_n--;					\
154	}								\
155} while (0)
156
157#if __BSD_VISIBLE
158#define swap16 __swap16
159#define swap32 __swap32
160#define swap64 __swap64
161#define swap16_multi __swap16_multi
162#endif /* __BSD_VISIBLE */
163
164#if _BYTE_ORDER == _LITTLE_ENDIAN
165
166/* Can be overridden by machine/endian.h before inclusion of this file.  */
167#ifndef _QUAD_HIGHWORD
168#define _QUAD_HIGHWORD 1
169#endif
170#ifndef _QUAD_LOWWORD
171#define _QUAD_LOWWORD 0
172#endif
173
174#if __BSD_VISIBLE
175#define htobe16 __swap16
176#define htobe32 __swap32
177#define htobe64 __swap64
178#define betoh16 __swap16
179#define betoh32 __swap32
180#define betoh64 __swap64
181
182#define htole16(x) (x)
183#define htole32(x) (x)
184#define htole64(x) (x)
185#define letoh16(x) (x)
186#define letoh32(x) (x)
187#define letoh64(x) (x)
188#endif /* __BSD_VISIBLE */
189
190#define htons(x) __swap16(x)
191#define htonl(x) __swap32(x)
192#define ntohs(x) __swap16(x)
193#define ntohl(x) __swap32(x)
194
195/* Bionic additions */
196#define ntohq(x) __swap64(x)
197#define htonq(x) __swap64(x)
198
199#define __LITTLE_ENDIAN_BITFIELD
200
201#endif /* _BYTE_ORDER */
202
203#if _BYTE_ORDER == _BIG_ENDIAN
204
205/* Can be overridden by machine/endian.h before inclusion of this file.  */
206#ifndef _QUAD_HIGHWORD
207#define _QUAD_HIGHWORD 0
208#endif
209#ifndef _QUAD_LOWWORD
210#define _QUAD_LOWWORD 1
211#endif
212
213#if __BSD_VISIBLE
214#define htole16 __swap16
215#define htole32 __swap32
216#define htole64 __swap64
217#define letoh16 __swap16
218#define letoh32 __swap32
219#define letoh64 __swap64
220
221#define htobe16(x) (x)
222#define htobe32(x) (x)
223#define htobe64(x) (x)
224#define betoh16(x) (x)
225#define betoh32(x) (x)
226#define betoh64(x) (x)
227#endif /* __BSD_VISIBLE */
228
229#define htons(x) (x)
230#define htonl(x) (x)
231#define ntohs(x) (x)
232#define ntohl(x) (x)
233
234/* Bionic additions */
235#define ntohq(x) (x)
236#define htonq(x) (x)
237
238#define __BIG_ENDIAN_BITFIELD
239
240#endif /* _BYTE_ORDER */
241
242#if __BSD_VISIBLE
243#define	NTOHL(x) (x) = ntohl((u_int32_t)(x))
244#define	NTOHS(x) (x) = ntohs((u_int16_t)(x))
245#define	HTONL(x) (x) = htonl((u_int32_t)(x))
246#define	HTONS(x) (x) = htons((u_int16_t)(x))
247#endif
248
249
250#define  __BYTE_ORDER       _BYTE_ORDER
251#ifndef  __LITTLE_ENDIAN
252#define  __LITTLE_ENDIAN    _LITTLE_ENDIAN
253#endif
254#ifndef  __BIG_ENDIAN
255#define  __BIG_ENDIAN       _BIG_ENDIAN
256#endif
257
258
259#ifdef __BSD_VISIBLE
260/*
261 * glibc-compatible beXXtoh/leXXtoh synonyms for htobeXX/htoleXX.
262 * The BSDs export both sets of names, bionic historically only
263 * exported the ones above (or on the rhs here), and glibc only
264 * exports these names (on the lhs).
265 */
266#define be16toh(x) htobe16(x)
267#define be32toh(x) htobe32(x)
268#define be64toh(x) htobe64(x)
269#define le16toh(x) htole16(x)
270#define le32toh(x) htole32(x)
271#define le64toh(x) htole64(x)
272#endif
273
274#endif /* _SYS_ENDIAN_H_ */
275