VirtualBox

source: vbox/trunk/src/recompiler_new/bswap.h@ 16254

Last change on this file since 16254 was 13382, checked in by vboxsync, 16 years ago

more MSVC-related stuff

  • Property svn:eol-style set to native
File size: 6.9 KB
Line 
1#ifndef BSWAP_H
2#define BSWAP_H
3
4#include "config-host.h"
5
6#ifndef _MSC_VER
7#include <inttypes.h>
8#endif
9
10#ifdef HAVE_BYTESWAP_H
11#include <byteswap.h>
12#else
13#ifdef _MSC_VER
14static _inline uint16_t bswap_16(register uint16_t x)
15{
16 return ((uint16_t)( \
17 (((uint16_t)(x) & (uint16_t)0x00ffU) << 8) | \
18 (((uint16_t)(x) & (uint16_t)0xff00U) >> 8) )); \
19}
20
21static _inline uint32_t bswap_32(register uint32_t x) \
22{ \
23 return ((uint32_t)( \
24 (((uint32_t)(x) & (uint32_t)0x000000ffUL) << 24) | \
25 (((uint32_t)(x) & (uint32_t)0x0000ff00UL) << 8) | \
26 (((uint32_t)(x) & (uint32_t)0x00ff0000UL) >> 8) | \
27 (((uint32_t)(x) & (uint32_t)0xff000000UL) >> 24) )); \
28}
29
30static _inline uint64_t bswap_64(register uint64_t x) \
31{ \
32 return ((uint64_t)( \
33 (uint64_t)(((uint64_t)(x) & (uint64_t)0x00000000000000ffULL) << 56) | \
34 (uint64_t)(((uint64_t)(x) & (uint64_t)0x000000000000ff00ULL) << 40) | \
35 (uint64_t)(((uint64_t)(x) & (uint64_t)0x0000000000ff0000ULL) << 24) | \
36 (uint64_t)(((uint64_t)(x) & (uint64_t)0x00000000ff000000ULL) << 8) | \
37 (uint64_t)(((uint64_t)(x) & (uint64_t)0x000000ff00000000ULL) >> 8) | \
38 (uint64_t)(((uint64_t)(x) & (uint64_t)0x0000ff0000000000ULL) >> 24) | \
39 (uint64_t)(((uint64_t)(x) & (uint64_t)0x00ff000000000000ULL) >> 40) | \
40 (uint64_t)(((uint64_t)(x) & (uint64_t)0xff00000000000000ULL) >> 56) )); \
41}
42
43#else
44
45#define bswap_16(x) __extension__ /* <- VBOX */ \
46({ \
47 uint16_t __x = (x); \
48 ((uint16_t)( \
49 (((uint16_t)(__x) & (uint16_t)0x00ffU) << 8) | \
50 (((uint16_t)(__x) & (uint16_t)0xff00U) >> 8) )); \
51})
52
53#define bswap_32(x) __extension__ /* <- VBOX */ \
54({ \
55 uint32_t __x = (x); \
56 ((uint32_t)( \
57 (((uint32_t)(__x) & (uint32_t)0x000000ffUL) << 24) | \
58 (((uint32_t)(__x) & (uint32_t)0x0000ff00UL) << 8) | \
59 (((uint32_t)(__x) & (uint32_t)0x00ff0000UL) >> 8) | \
60 (((uint32_t)(__x) & (uint32_t)0xff000000UL) >> 24) )); \
61})
62
63#define bswap_64(x) __extension__ /* <- VBOX */ \
64({ \
65 uint64_t __x = (x); \
66 ((uint64_t)( \
67 (uint64_t)(((uint64_t)(__x) & (uint64_t)0x00000000000000ffULL) << 56) | \
68 (uint64_t)(((uint64_t)(__x) & (uint64_t)0x000000000000ff00ULL) << 40) | \
69 (uint64_t)(((uint64_t)(__x) & (uint64_t)0x0000000000ff0000ULL) << 24) | \
70 (uint64_t)(((uint64_t)(__x) & (uint64_t)0x00000000ff000000ULL) << 8) | \
71 (uint64_t)(((uint64_t)(__x) & (uint64_t)0x000000ff00000000ULL) >> 8) | \
72 (uint64_t)(((uint64_t)(__x) & (uint64_t)0x0000ff0000000000ULL) >> 24) | \
73 (uint64_t)(((uint64_t)(__x) & (uint64_t)0x00ff000000000000ULL) >> 40) | \
74 (uint64_t)(((uint64_t)(__x) & (uint64_t)0xff00000000000000ULL) >> 56) )); \
75})
76#endif
77
78#endif /* !HAVE_BYTESWAP_H */
79
80#ifndef bswap16 /* BSD endian.h clash */
81#ifndef VBOX
82static inline uint16_t bswap16(uint16_t x)
83#else
84DECLINLINE(uint16_t) bswap16(uint16_t x)
85#endif
86{
87 return bswap_16(x);
88}
89#endif
90
91#ifndef bswap32 /* BSD endian.h clash */
92#ifndef VBOX
93static inline uint32_t bswap32(uint32_t x)
94#else
95DECLINLINE(uint32_t) bswap32(uint32_t x)
96#endif
97{
98 return bswap_32(x);
99}
100#endif
101
102#ifndef bswap64 /* BSD endian.h clash. */
103#ifndef VBOX
104static inline uint64_t bswap64(uint64_t x)
105#else
106DECLINLINE(uint64_t) bswap64(uint64_t x)
107#endif
108{
109 return bswap_64(x);
110}
111#endif
112
113#ifndef VBOX
114static inline void bswap16s(uint16_t *s)
115#else
116DECLINLINE(void) bswap16s(uint16_t *s)
117#endif
118{
119 *s = bswap16(*s);
120}
121
122#ifndef VBOX
123static inline void bswap32s(uint32_t *s)
124#else
125DECLINLINE(void) bswap32s(uint32_t *s)
126#endif
127{
128 *s = bswap32(*s);
129}
130
131#ifndef VBOX
132static inline void bswap64s(uint64_t *s)
133#else
134DECLINLINE(void) bswap64s(uint64_t *s)
135#endif
136{
137 *s = bswap64(*s);
138}
139
140#if defined(WORDS_BIGENDIAN)
141#define be_bswap(v, size) (v)
142#define le_bswap(v, size) bswap ## size(v)
143#define be_bswaps(v, size)
144#define le_bswaps(p, size) *p = bswap ## size(*p);
145#else
146#define le_bswap(v, size) (v)
147#define be_bswap(v, size) bswap ## size(v)
148#define le_bswaps(v, size)
149#define be_bswaps(p, size) *p = bswap ## size(*p);
150#endif
151
152#ifndef VBOX
153#define CPU_CONVERT(endian, size, type)\
154static inline type endian ## size ## _to_cpu(type v)\
155{\
156 return endian ## _bswap(v, size);\
157}\
158\
159static inline type cpu_to_ ## endian ## size(type v)\
160{\
161 return endian ## _bswap(v, size);\
162}\
163\
164static inline void endian ## size ## _to_cpus(type *p)\
165{\
166 endian ## _bswaps(p, size)\
167}\
168\
169static inline void cpu_to_ ## endian ## size ## s(type *p)\
170{\
171 endian ## _bswaps(p, size)\
172}\
173\
174static inline type endian ## size ## _to_cpup(const type *p)\
175{\
176 return endian ## size ## _to_cpu(*p);\
177}\
178\
179static inline void cpu_to_ ## endian ## size ## w(type *p, type v)\
180{\
181 *p = cpu_to_ ## endian ## size(v);\
182}
183#else /* VBOX */
184#define CPU_CONVERT(endian, size, type)\
185DECLINLINE(type) endian ## size ## _to_cpu(type v)\
186{\
187 return endian ## _bswap(v, size);\
188}\
189\
190DECLINLINE(type) cpu_to_ ## endian ## size(type v)\
191{\
192 return endian ## _bswap(v, size);\
193}\
194\
195DECLINLINE(void) endian ## size ## _to_cpus(type *p)\
196{\
197 endian ## _bswaps(p, size)\
198}\
199\
200DECLINLINE(void) cpu_to_ ## endian ## size ## s(type *p)\
201{\
202 endian ## _bswaps(p, size)\
203}\
204\
205DECLINLINE(type) endian ## size ## _to_cpup(const type *p)\
206{\
207 return endian ## size ## _to_cpu(*p);\
208}\
209\
210DECLINLINE(void) cpu_to_ ## endian ## size ## w(type *p, type v)\
211{\
212 *p = cpu_to_ ## endian ## size(v);\
213}
214#endif /* VBOX */
215
216CPU_CONVERT(be, 16, uint16_t)
217CPU_CONVERT(be, 32, uint32_t)
218CPU_CONVERT(be, 64, uint64_t)
219
220CPU_CONVERT(le, 16, uint16_t)
221CPU_CONVERT(le, 32, uint32_t)
222CPU_CONVERT(le, 64, uint64_t)
223
224/* unaligned versions (optimized for frequent unaligned accesses)*/
225
226#if defined(__i386__) || defined(__powerpc__)
227
228#define cpu_to_le16wu(p, v) cpu_to_le16w(p, v)
229#define cpu_to_le32wu(p, v) cpu_to_le32w(p, v)
230#define le16_to_cpupu(p) le16_to_cpup(p)
231#define le32_to_cpupu(p) le32_to_cpup(p)
232
233#define cpu_to_be16wu(p, v) cpu_to_be16w(p, v)
234#define cpu_to_be32wu(p, v) cpu_to_be32w(p, v)
235
236#else
237
238static inline void cpu_to_le16wu(uint16_t *p, uint16_t v)
239{
240 uint8_t *p1 = (uint8_t *)p;
241
242 p1[0] = (uint8_t)v;
243 p1[1] = v >> 8;
244}
245
246static inline void cpu_to_le32wu(uint32_t *p, uint32_t v)
247{
248 uint8_t *p1 = (uint8_t *)p;
249
250 p1[0] = (uint8_t)v;
251 p1[1] = v >> 8;
252 p1[2] = v >> 16;
253 p1[3] = v >> 24;
254}
255
256static inline uint16_t le16_to_cpupu(const uint16_t *p)
257{
258 const uint8_t *p1 = (const uint8_t *)p;
259 return p1[0] | (p1[1] << 8);
260}
261
262static inline uint32_t le32_to_cpupu(const uint32_t *p)
263{
264 const uint8_t *p1 = (const uint8_t *)p;
265 return p1[0] | (p1[1] << 8) | (p1[2] << 16) | (p1[3] << 24);
266}
267
268static inline void cpu_to_be16wu(uint16_t *p, uint16_t v)
269{
270 uint8_t *p1 = (uint8_t *)p;
271
272 p1[0] = v >> 8;
273 p1[1] = (uint8_t)v;
274}
275
276static inline void cpu_to_be32wu(uint32_t *p, uint32_t v)
277{
278 uint8_t *p1 = (uint8_t *)p;
279
280 p1[0] = v >> 24;
281 p1[1] = v >> 16;
282 p1[2] = v >> 8;
283 p1[3] = (uint8_t)v;
284}
285
286#endif
287
288#ifdef WORDS_BIGENDIAN
289#define cpu_to_32wu cpu_to_be32wu
290#else
291#define cpu_to_32wu cpu_to_le32wu
292#endif
293
294#undef le_bswap
295#undef be_bswap
296#undef le_bswaps
297#undef be_bswaps
298
299#endif /* BSWAP_H */
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette