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