]>
git.proxmox.com Git - mirror_edk2.git/blob - StdLib/Include/sys/endian.h
2 Byte order related definitions and declarations.
4 Copyright (c) 2010 - 2011, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials are licensed and made available
6 under the terms and conditions of the BSD License that accompanies this
7 distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13 Copyright (c) 1990, 1993
14 The Regents of the University of California. All rights reserved.
16 Redistribution and use in source and binary forms, with or without
17 modification, are permitted provided that the following conditions
19 - Redistributions of source code must retain the above copyright
20 notice, this list of conditions and the following disclaimer.
21 - Redistributions in binary form must reproduce the above copyright
22 notice, this list of conditions and the following disclaimer in the
23 documentation and/or other materials provided with the distribution.
24 - Neither the name of the University nor the names of its contributors
25 may be used to endorse or promote products derived from this software
26 without specific prior written permission.
28 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
29 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
32 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 POSSIBILITY OF SUCH DAMAGE.
40 NetBSD: endian.h,v 1.24 2006/05/05 15:08:11 christos Exp
41 endian.h 8.1 (Berkeley) 6/11/93
43 #ifndef _SYS_ENDIAN_H_
44 #define _SYS_ENDIAN_H_
46 #include <sys/EfiCdefs.h>
49 * Definitions for byte order, according to byte significance from low
52 #define _LITTLE_ENDIAN 1234 /* LSB first: i386, vax */
53 #define _BIG_ENDIAN 4321 /* MSB first: 68000, ibm, net */
54 #define _PDP_ENDIAN 3412 /* LSB first in word, MSW first in long */
57 #if defined(_XOPEN_SOURCE) || defined(_NETBSD_SOURCE)
60 /* C-family endian-ness definitions */
63 #include <sys/types.h>
66 typedef __in_addr_t in_addr_t
;
67 #define in_addr_t __in_addr_t
71 typedef __in_port_t in_port_t
;
72 #define in_port_t __in_port_t
76 uint32_t htonl(uint32_t) __attribute__((__const__
));
77 uint16_t htons(uint16_t) __attribute__((__const__
));
78 uint32_t ntohl(uint32_t) __attribute__((__const__
));
79 uint16_t ntohs(uint16_t) __attribute__((__const__
));
83 #endif /* _XOPEN_SOURCE || _NETBSD_SOURCE */
86 #include <machine/endian_machdep.h>
89 * Define the order of 32-bit words in 64-bit words.
91 #if _BYTE_ORDER == _LITTLE_ENDIAN
92 #define _QUAD_HIGHWORD 1
93 #define _QUAD_LOWWORD 0
96 #if _BYTE_ORDER == _BIG_ENDIAN
97 #define _QUAD_HIGHWORD 0
98 #define _QUAD_LOWWORD 1
102 #if defined(_XOPEN_SOURCE) || defined(_NETBSD_SOURCE)
104 * Traditional names for byteorder. These are defined as the numeric
105 * sequences so that third party code can "#define XXX_ENDIAN" and not
108 #define LITTLE_ENDIAN 1234 /* LSB first: i386, vax */
109 #define BIG_ENDIAN 4321 /* MSB first: 68000, ibm, net */
110 #define PDP_ENDIAN 3412 /* LSB first in word, MSW first in long */
111 #define BYTE_ORDER _BYTE_ORDER
115 #include <machine/bswap.h>
118 * Macros for network/external number representation conversion.
120 #if BYTE_ORDER == BIG_ENDIAN && !defined(__lint__)
126 #define NTOHL(x) (void) (x)
127 #define NTOHS(x) (void) (x)
128 #define HTONL(x) (void) (x)
129 #define HTONS(x) (void) (x)
131 #else /* LITTLE_ENDIAN || !defined(__lint__) */
133 #define ntohl(x) bswap32((uint32_t)(x))
134 #define ntohs(x) bswap16((uint16_t)(x))
135 #define htonl(x) bswap32((uint32_t)(x))
136 #define htons(x) bswap16((uint16_t)(x))
138 #define NTOHL(x) (x) = ntohl((uint32_t)(x))
139 #define NTOHS(x) (x) = ntohs((uint16_t)(x))
140 #define HTONL(x) (x) = htonl((uint32_t)(x))
141 #define HTONS(x) (x) = htons((uint16_t)(x))
142 #endif /* LITTLE_ENDIAN || !defined(__lint__) */
145 * Macros to convert to a specific endianness.
148 #if BYTE_ORDER == BIG_ENDIAN
150 #define htobe16(x) (x)
151 #define htobe32(x) (x)
152 #define htobe64(x) (x)
153 #define htole16(x) bswap16((uint16_t)(x))
154 #define htole32(x) bswap32((uint32_t)(x))
155 #define htole64(x) bswap64((uint64_t)(x))
157 #define HTOBE16(x) (void) (x)
158 #define HTOBE32(x) (void) (x)
159 #define HTOBE64(x) (void) (x)
160 #define HTOLE16(x) (x) = bswap16((uint16_t)(x))
161 #define HTOLE32(x) (x) = bswap32((uint32_t)(x))
162 #define HTOLE64(x) (x) = bswap64((uint64_t)(x))
164 #else /* LITTLE_ENDIAN */
166 #define htobe16(x) bswap16((uint16_t)(x))
167 #define htobe32(x) bswap32((uint32_t)(x))
168 #define htobe64(x) bswap64((uint64_t)(x))
169 #define htole16(x) (x)
170 #define htole32(x) (x)
171 #define htole64(x) (x)
173 #define HTOBE16(x) (x) = bswap16((uint16_t)(x))
174 #define HTOBE32(x) (x) = bswap32((uint32_t)(x))
175 #define HTOBE64(x) (x) = bswap64((uint64_t)(x))
176 #define HTOLE16(x) (void) (x)
177 #define HTOLE32(x) (void) (x)
178 #define HTOLE64(x) (void) (x)
180 #endif /* LITTLE_ENDIAN */
182 #define be16toh(x) htobe16(x)
183 #define be32toh(x) htobe32(x)
184 #define be64toh(x) htobe64(x)
185 #define le16toh(x) htole16(x)
186 #define le32toh(x) htole32(x)
187 #define le64toh(x) htole64(x)
189 #define BE16TOH(x) HTOBE16(x)
190 #define BE32TOH(x) HTOBE32(x)
191 #define BE64TOH(x) HTOBE64(x)
192 #define LE16TOH(x) HTOLE16(x)
193 #define LE32TOH(x) HTOLE32(x)
194 #define LE64TOH(x) HTOLE64(x)
197 * Routines to encode/decode big- and little-endian multi-octet values
198 * to/from an octet stream.
201 static __inline
void __unused
202 be16enc(void *buf
, uint16_t u
)
204 uint8_t *p
= (uint8_t *)buf
;
206 p
[0] = (uint8_t)(((unsigned)u
>> 8) & 0xff);
207 p
[1] = (uint8_t)(u
& 0xff);
210 static __inline
void __unused
211 le16enc(void *buf
, uint16_t u
)
213 uint8_t *p
= (uint8_t *)buf
;
215 p
[0] = (uint8_t)(u
& 0xff);
216 p
[1] = (uint8_t)(((unsigned)u
>> 8) & 0xff);
219 static __inline
uint16_t __unused
220 be16dec(const void *buf
)
222 const uint8_t *p
= (const uint8_t *)buf
;
224 return ((p
[0] << 8) | p
[1]);
227 static __inline
uint16_t __unused
228 le16dec(const void *buf
)
230 const uint8_t *p
= (const uint8_t *)buf
;
232 return ((p
[1] << 8) | p
[0]);
235 static __inline
void __unused
236 be32enc(void *buf
, uint32_t u
)
238 uint8_t *p
= (uint8_t *)buf
;
240 p
[0] = (uint8_t)((u
>> 24) & 0xff);
241 p
[1] = (uint8_t)((u
>> 16) & 0xff);
242 p
[2] = (uint8_t)((u
>> 8) & 0xff);
243 p
[3] = (uint8_t)(u
& 0xff);
246 static __inline
void __unused
247 le32enc(void *buf
, uint32_t u
)
249 uint8_t *p
= (uint8_t *)buf
;
251 p
[0] = (uint8_t)(u
& 0xff);
252 p
[1] = (uint8_t)((u
>> 8) & 0xff);
253 p
[2] = (uint8_t)((u
>> 16) & 0xff);
254 p
[3] = (uint8_t)((u
>> 24) & 0xff);
257 static __inline
uint32_t __unused
258 be32dec(const void *buf
)
260 const uint8_t *p
= (const uint8_t *)buf
;
262 return ((p
[0] << 24) | (p
[1] << 16) | (p
[2] << 8) | p
[3]);
265 static __inline
uint32_t __unused
266 le32dec(const void *buf
)
268 const uint8_t *p
= (const uint8_t *)buf
;
270 return ((p
[3] << 24) | (p
[2] << 16) | (p
[1] << 8) | p
[0]);
273 static __inline
void __unused
274 be64enc(void *buf
, uint64_t u
)
276 uint8_t *p
= (uint8_t *)buf
;
278 be32enc(p
, (uint32_t)(u
>> 32));
279 be32enc(p
+ 4, (uint32_t)(u
& 0xffffffffULL
));
282 static __inline
void __unused
283 le64enc(void *buf
, uint64_t u
)
285 uint8_t *p
= (uint8_t *)buf
;
287 le32enc(p
, (uint32_t)(u
& 0xffffffffULL
));
288 le32enc(p
+ 4, (uint32_t)(u
>> 32));
291 static __inline
uint64_t __unused
292 be64dec(const void *buf
)
294 const uint8_t *p
= (const uint8_t *)buf
;
296 return (((uint64_t)be32dec(p
) << 32) | be32dec(p
+ 4));
299 static __inline
uint64_t __unused
300 le64dec(const void *buf
)
302 const uint8_t *p
= (const uint8_t *)buf
;
304 return (le32dec(p
) | ((uint64_t)le32dec(p
+ 4) << 32));
307 #endif /* !_LOCORE */
308 #endif /* _XOPEN_SOURCE || _NETBSD_SOURCE */
309 #endif /* !_SYS_ENDIAN_H_ */