RTEMS 6.1-rc1
endian.h
Go to the documentation of this file.
1
10/*-
11 * Copyright (c) 2002 Thomas Moestl <tmm@FreeBSD.org>
12 * All rights reserved.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * $FreeBSD: head/sys/sys/endian.h 208331 2010-05-20 06:16:13Z phk $
36 */
37
38#ifndef _SYS_ENDIAN_H_
39#define _SYS_ENDIAN_H_
40
41#include <sys/cdefs.h>
42#include <sys/_types.h>
43#include <machine/endian.h>
44
45#ifndef _UINT8_T_DECLARED
46typedef __uint8_t uint8_t;
47#define _UINT8_T_DECLARED
48#endif
49
50#ifndef _UINT16_T_DECLARED
51typedef __uint16_t uint16_t;
52#define _UINT16_T_DECLARED
53#endif
54
55#ifndef _UINT32_T_DECLARED
56typedef __uint32_t uint32_t;
57#define _UINT32_T_DECLARED
58#endif
59
60#ifndef _UINT64_T_DECLARED
61typedef __uint64_t uint64_t;
62#define _UINT64_T_DECLARED
63#endif
64
65/*
66 * General byte order swapping functions.
67 */
68#define bswap16(x) __bswap16(x)
69#define bswap32(x) __bswap32(x)
70#define bswap64(x) __bswap64(x)
71
72/*
73 * Host to big endian, host to little endian, big endian to host, and little
74 * endian to host byte order functions as detailed in byteorder(9).
75 */
76#if _BYTE_ORDER == _LITTLE_ENDIAN
77#define htobe16(x) bswap16((x))
78#define htobe32(x) bswap32((x))
79#define htobe64(x) bswap64((x))
80#define htole16(x) ((uint16_t)(x))
81#define htole32(x) ((uint32_t)(x))
82#define htole64(x) ((uint64_t)(x))
83
84#define be16toh(x) bswap16((x))
85#define be32toh(x) bswap32((x))
86#define be64toh(x) bswap64((x))
87#define le16toh(x) ((uint16_t)(x))
88#define le32toh(x) ((uint32_t)(x))
89#define le64toh(x) ((uint64_t)(x))
90#else /* _BYTE_ORDER != _LITTLE_ENDIAN */
91#define htobe16(x) ((uint16_t)(x))
92#define htobe32(x) ((uint32_t)(x))
93#define htobe64(x) ((uint64_t)(x))
94#define htole16(x) bswap16((x))
95#define htole32(x) bswap32((x))
96#define htole64(x) bswap64((x))
97
98#define be16toh(x) ((uint16_t)(x))
99#define be32toh(x) ((uint32_t)(x))
100#define be64toh(x) ((uint64_t)(x))
101#define le16toh(x) bswap16((x))
102#define le32toh(x) bswap32((x))
103#define le64toh(x) bswap64((x))
104#endif /* _BYTE_ORDER == _LITTLE_ENDIAN */
105
106/* Alignment-agnostic encode/decode bytestream to/from little/big endian. */
107
108static __inline uint16_t
109be16dec(const void *pp)
110{
111 uint8_t const *p = (uint8_t const *)pp;
112
113 return (((unsigned)p[0] << 8) | p[1]);
114}
115
116static __inline uint32_t
117be32dec(const void *pp)
118{
119 uint8_t const *p = (uint8_t const *)pp;
120
121 return (((uint32_t)p[0] << 24) | ((uint32_t)p[1] << 16) |
122 ((uint32_t)p[2] << 8) | p[3]);
123}
124
125static __inline uint64_t
126be64dec(const void *pp)
127{
128 uint8_t const *p = (uint8_t const *)pp;
129
130 return (((uint64_t)be32dec(p) << 32) | be32dec(p + 4));
131}
132
133static __inline uint16_t
134le16dec(const void *pp)
135{
136 uint8_t const *p = (uint8_t const *)pp;
137
138 return (((unsigned)p[1] << 8) | p[0]);
139}
140
141static __inline uint32_t
142le32dec(const void *pp)
143{
144 uint8_t const *p = (uint8_t const *)pp;
145
146 return (((uint32_t)p[3] << 24) | ((uint32_t)p[2] << 16) |
147 ((uint32_t)p[1] << 8) | p[0]);
148}
149
150static __inline uint64_t
151le64dec(const void *pp)
152{
153 uint8_t const *p = (uint8_t const *)pp;
154
155 return (((uint64_t)le32dec(p + 4) << 32) | le32dec(p));
156}
157
158static __inline void
159be16enc(void *pp, uint16_t u)
160{
161 uint8_t *p = (uint8_t *)pp;
162
163 p[0] = (u >> 8) & 0xff;
164 p[1] = u & 0xff;
165}
166
167static __inline void
168be32enc(void *pp, uint32_t u)
169{
170 uint8_t *p = (uint8_t *)pp;
171
172 p[0] = (u >> 24) & 0xff;
173 p[1] = (u >> 16) & 0xff;
174 p[2] = (u >> 8) & 0xff;
175 p[3] = u & 0xff;
176}
177
178static __inline void
179be64enc(void *pp, uint64_t u)
180{
181 uint8_t *p = (uint8_t *)pp;
182
183 be32enc(p, (uint32_t)(u >> 32));
184 be32enc(p + 4, (uint32_t)(u & 0xffffffffU));
185}
186
187static __inline void
188le16enc(void *pp, uint16_t u)
189{
190 uint8_t *p = (uint8_t *)pp;
191
192 p[0] = u & 0xff;
193 p[1] = (u >> 8) & 0xff;
194}
195
196static __inline void
197le32enc(void *pp, uint32_t u)
198{
199 uint8_t *p = (uint8_t *)pp;
200
201 p[0] = u & 0xff;
202 p[1] = (u >> 8) & 0xff;
203 p[2] = (u >> 16) & 0xff;
204 p[3] = (u >> 24) & 0xff;
205}
206
207static __inline void
208le64enc(void *pp, uint64_t u)
209{
210 uint8_t *p = (uint8_t *)pp;
211
212 le32enc(p, (uint32_t)(u & 0xffffffffU));
213 le32enc(p + 4, (uint32_t)(u >> 32));
214}
215
216#endif /* _SYS_ENDIAN_H_ */
unsigned p
Definition: tte.h:17