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