1 |
1254 |
phoenix |
//==========================================================================
|
2 |
|
|
//
|
3 |
|
|
// include/sys/endian.h
|
4 |
|
|
//
|
5 |
|
|
//
|
6 |
|
|
//
|
7 |
|
|
//==========================================================================
|
8 |
|
|
//####BSDCOPYRIGHTBEGIN####
|
9 |
|
|
//
|
10 |
|
|
// -------------------------------------------
|
11 |
|
|
//
|
12 |
|
|
// Portions of this software may have been derived from OpenBSD or other sources,
|
13 |
|
|
// and are covered by the appropriate copyright disclaimers included herein.
|
14 |
|
|
//
|
15 |
|
|
// -------------------------------------------
|
16 |
|
|
//
|
17 |
|
|
//####BSDCOPYRIGHTEND####
|
18 |
|
|
//==========================================================================
|
19 |
|
|
//#####DESCRIPTIONBEGIN####
|
20 |
|
|
//
|
21 |
|
|
// Author(s): gthomas
|
22 |
|
|
// Contributors: gthomas
|
23 |
|
|
// Date: 2000-01-10
|
24 |
|
|
// Purpose:
|
25 |
|
|
// Description:
|
26 |
|
|
//
|
27 |
|
|
//
|
28 |
|
|
//####DESCRIPTIONEND####
|
29 |
|
|
//
|
30 |
|
|
//==========================================================================
|
31 |
|
|
|
32 |
|
|
|
33 |
|
|
/* $OpenBSD: endian.h,v 1.4 1999/07/21 05:58:25 csapuntz Exp $ */
|
34 |
|
|
|
35 |
|
|
/*-
|
36 |
|
|
* Copyright (c) 1997 Niklas Hallqvist. All rights reserved.
|
37 |
|
|
*
|
38 |
|
|
* Redistribution and use in source and binary forms, with or without
|
39 |
|
|
* modification, are permitted provided that the following conditions
|
40 |
|
|
* are met:
|
41 |
|
|
* 1. Redistributions of source code must retain the above copyright
|
42 |
|
|
* notice, this list of conditions and the following disclaimer.
|
43 |
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
44 |
|
|
* notice, this list of conditions and the following disclaimer in the
|
45 |
|
|
* documentation and/or other materials provided with the distribution.
|
46 |
|
|
* 3. All advertising materials mentioning features or use of this software
|
47 |
|
|
* must display the following acknowledgement:
|
48 |
|
|
* This product includes software developed by Niklas Hallqvist.
|
49 |
|
|
* 4. The name of the author may not be used to endorse or promote products
|
50 |
|
|
* derived from this software without specific prior written permission.
|
51 |
|
|
*
|
52 |
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
53 |
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
54 |
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
55 |
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
56 |
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
57 |
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
58 |
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
59 |
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
60 |
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
61 |
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
62 |
|
|
*/
|
63 |
|
|
|
64 |
|
|
/*
|
65 |
|
|
* Generic definitions for little- and big-endian systems. Other endianesses
|
66 |
|
|
* has to be dealt with in the specific machine/endian.h file for that port.
|
67 |
|
|
*
|
68 |
|
|
* This file is meant to be included from a little- or big-endian port's
|
69 |
|
|
* machine/endian.h after setting BYTE_ORDER to either 1234 for little endian
|
70 |
|
|
* or 4321 for big..
|
71 |
|
|
*/
|
72 |
|
|
|
73 |
|
|
#ifndef _SYS_ENDIAN_H_
|
74 |
|
|
#define _SYS_ENDIAN_H_
|
75 |
|
|
|
76 |
|
|
#ifndef _POSIX_SOURCE
|
77 |
|
|
|
78 |
|
|
#include <sys/cdefs.h>
|
79 |
|
|
|
80 |
|
|
#define LITTLE_ENDIAN 1234
|
81 |
|
|
|
82 |
|
|
|
83 |
|
|
#define BIG_ENDIAN 4321
|
84 |
|
|
#define PDP_ENDIAN 3412
|
85 |
|
|
|
86 |
|
|
#ifdef __GNUC__
|
87 |
|
|
|
88 |
|
|
#define __swap16gen(x) ({ \
|
89 |
|
|
u_int16_t __swap16gen_x = (x); \
|
90 |
|
|
\
|
91 |
|
|
(u_int16_t)((__swap16gen_x & 0xff) << 8 | \
|
92 |
|
|
(__swap16gen_x & 0xff00) >> 8); \
|
93 |
|
|
})
|
94 |
|
|
|
95 |
|
|
#define __swap32gen(x) ({ \
|
96 |
|
|
u_int32_t __swap32gen_x = (x); \
|
97 |
|
|
\
|
98 |
|
|
(u_int32_t)((__swap32gen_x & 0xff) << 24 | \
|
99 |
|
|
(__swap32gen_x & 0xff00) << 8 | \
|
100 |
|
|
(__swap32gen_x & 0xff0000) >> 8 | \
|
101 |
|
|
(__swap32gen_x & 0xff000000) >> 24); \
|
102 |
|
|
})
|
103 |
|
|
|
104 |
|
|
#else /* __GNUC__ */
|
105 |
|
|
|
106 |
|
|
/* Note that these macros evaluate their arguments several times. */
|
107 |
|
|
#define __swap16gen(x) \
|
108 |
|
|
(u_int16_t)(((u_int16_t)(x) & 0xff) << 8 | ((u_int16_t)(x) & 0xff00) >> 8)
|
109 |
|
|
|
110 |
|
|
#define __swap32gen(x) \
|
111 |
|
|
(u_int32_t)(((u_int32_t)(x) & 0xff) << 24 | \
|
112 |
|
|
((u_int32_t)(x) & 0xff00) << 8 | ((u_int32_t)(x) & 0xff0000) >> 8 | \
|
113 |
|
|
((u_int32_t)(x) & 0xff000000) >> 24)
|
114 |
|
|
|
115 |
|
|
#endif /* __GNUC__ */
|
116 |
|
|
|
117 |
|
|
/*
|
118 |
|
|
* Define MD_SWAP if you provide swap{16,32}md functions/macros that are
|
119 |
|
|
* optimized for your architecture, These will be used for swap{16,32}
|
120 |
|
|
* unless the argument is a constant and we are using GCC, where we can
|
121 |
|
|
* take advantage of the CSE phase much better by using the generic version.
|
122 |
|
|
*/
|
123 |
|
|
#ifdef MD_SWAP
|
124 |
|
|
#if __GNUC__
|
125 |
|
|
|
126 |
|
|
#define swap16(x) ({ \
|
127 |
|
|
u_int16_t __swap16_x = (x); \
|
128 |
|
|
\
|
129 |
|
|
__builtin_constant_p(x) ? __swap16gen(__swap16_x) : \
|
130 |
|
|
__swap16md(__swap16_x); \
|
131 |
|
|
})
|
132 |
|
|
|
133 |
|
|
#define swap32(x) ({ \
|
134 |
|
|
u_int32_t __swap32_x = (x); \
|
135 |
|
|
\
|
136 |
|
|
__builtin_constant_p(x) ? __swap32gen(__swap32_x) : \
|
137 |
|
|
__swap32md(__swap32_x); \
|
138 |
|
|
})
|
139 |
|
|
|
140 |
|
|
#endif /* __GNUC__ */
|
141 |
|
|
|
142 |
|
|
#else /* MD_SWAP */
|
143 |
|
|
#define swap16 __swap16gen
|
144 |
|
|
#define swap32 __swap32gen
|
145 |
|
|
#endif /* MD_SWAP */
|
146 |
|
|
|
147 |
|
|
#define swap16_multi(v, n) do { \
|
148 |
|
|
size_t __swap16_multi_n = (n); \
|
149 |
|
|
u_int16_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 |
|
|
__BEGIN_DECLS
|
159 |
|
|
u_int32_t htobe32 __P((u_int32_t));
|
160 |
|
|
u_int16_t htobe16 __P((u_int16_t));
|
161 |
|
|
u_int32_t betoh32 __P((u_int32_t));
|
162 |
|
|
u_int16_t betoh16 __P((u_int16_t));
|
163 |
|
|
|
164 |
|
|
u_int32_t htole32 __P((u_int32_t));
|
165 |
|
|
u_int16_t htole16 __P((u_int16_t));
|
166 |
|
|
u_int32_t letoh32 __P((u_int32_t));
|
167 |
|
|
u_int16_t letoh16 __P((u_int16_t));
|
168 |
|
|
__END_DECLS
|
169 |
|
|
|
170 |
|
|
#if BYTE_ORDER == LITTLE_ENDIAN
|
171 |
|
|
|
172 |
|
|
/* Can be overridden by machine/endian.h before inclusion of this file. */
|
173 |
|
|
#ifndef _QUAD_HIGHWORD
|
174 |
|
|
#define _QUAD_HIGHWORD 1
|
175 |
|
|
#endif
|
176 |
|
|
#ifndef _QUAD_LOWWORD
|
177 |
|
|
#define _QUAD_LOWWORD 0
|
178 |
|
|
#endif
|
179 |
|
|
|
180 |
|
|
#define htobe16 swap16
|
181 |
|
|
#define htobe32 swap32
|
182 |
|
|
#define betoh16 swap16
|
183 |
|
|
#define betoh32 swap32
|
184 |
|
|
|
185 |
|
|
#define htole16(x) (x)
|
186 |
|
|
#define htole32(x) (x)
|
187 |
|
|
#define letoh16(x) (x)
|
188 |
|
|
#define letoh32(x) (x)
|
189 |
|
|
|
190 |
|
|
#endif /* BYTE_ORDER */
|
191 |
|
|
|
192 |
|
|
#if BYTE_ORDER == BIG_ENDIAN
|
193 |
|
|
|
194 |
|
|
/* Can be overridden by machine/endian.h before inclusion of this file. */
|
195 |
|
|
#ifndef _QUAD_HIGHWORD
|
196 |
|
|
#define _QUAD_HIGHWORD 0
|
197 |
|
|
#endif
|
198 |
|
|
#ifndef _QUAD_LOWWORD
|
199 |
|
|
#define _QUAD_LOWWORD 1
|
200 |
|
|
#endif
|
201 |
|
|
|
202 |
|
|
#define htole16 swap16
|
203 |
|
|
#define htole32 swap32
|
204 |
|
|
#define letoh16 swap16
|
205 |
|
|
#define letoh32 swap32
|
206 |
|
|
|
207 |
|
|
#define htobe16(x) (x)
|
208 |
|
|
#define htobe32(x) (x)
|
209 |
|
|
#define betoh16(x) (x)
|
210 |
|
|
#define betoh32(x) (x)
|
211 |
|
|
|
212 |
|
|
#endif /* BYTE_ORDER */
|
213 |
|
|
|
214 |
|
|
#define htons htobe16
|
215 |
|
|
#define htonl htobe32
|
216 |
|
|
#define ntohs betoh16
|
217 |
|
|
#define ntohl betoh32
|
218 |
|
|
|
219 |
|
|
#define NTOHL(x) (x) = ntohl((u_int32_t)(x))
|
220 |
|
|
#define NTOHS(x) (x) = ntohs((u_int16_t)(x))
|
221 |
|
|
#define HTONL(x) (x) = htonl((u_int32_t)(x))
|
222 |
|
|
#define HTONS(x) (x) = htons((u_int16_t)(x))
|
223 |
|
|
|
224 |
|
|
#endif /* _POSIX_SOURCE */
|
225 |
|
|
#endif /* _SYS_ENDIAN_H_ */
|