VirtualBox

source: vbox/trunk/src/recompiler/bswap.h@ 1123

Last change on this file since 1123 was 1, checked in by vboxsync, 55 years ago

import

  • Property svn:eol-style set to native
File size: 5.7 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) \
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) \
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) \
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 */
81static inline uint16_t bswap16(uint16_t x)
82{
83 return bswap_16(x);
84}
85#endif
86
87#ifndef bswap32 /* BSD endian.h clash */
88static inline uint32_t bswap32(uint32_t x)
89{
90 return bswap_32(x);
91}
92#endif
93
94#ifndef bswap64 /* BSD endian.h clash. */
95static inline uint64_t bswap64(uint64_t x)
96{
97 return bswap_64(x);
98}
99#endif
100
101static inline void bswap16s(uint16_t *s)
102{
103 *s = bswap16(*s);
104}
105
106static inline void bswap32s(uint32_t *s)
107{
108 *s = bswap32(*s);
109}
110
111static inline void bswap64s(uint64_t *s)
112{
113 *s = bswap64(*s);
114}
115
116#if defined(WORDS_BIGENDIAN)
117#define be_bswap(v, size) (v)
118#define le_bswap(v, size) bswap ## size(v)
119#define be_bswaps(v, size)
120#define le_bswaps(p, size) *p = bswap ## size(*p);
121#else
122#define le_bswap(v, size) (v)
123#define be_bswap(v, size) bswap ## size(v)
124#define le_bswaps(v, size)
125#define be_bswaps(p, size) *p = bswap ## size(*p);
126#endif
127
128#define CPU_CONVERT(endian, size, type)\
129static inline type endian ## size ## _to_cpu(type v)\
130{\
131 return endian ## _bswap(v, size);\
132}\
133\
134static inline type cpu_to_ ## endian ## size(type v)\
135{\
136 return endian ## _bswap(v, size);\
137}\
138\
139static inline void endian ## size ## _to_cpus(type *p)\
140{\
141 endian ## _bswaps(p, size)\
142}\
143\
144static inline void cpu_to_ ## endian ## size ## s(type *p)\
145{\
146 endian ## _bswaps(p, size)\
147}\
148\
149static inline type endian ## size ## _to_cpup(const type *p)\
150{\
151 return endian ## size ## _to_cpu(*p);\
152}\
153\
154static inline void cpu_to_ ## endian ## size ## w(type *p, type v)\
155{\
156 *p = cpu_to_ ## endian ## size(v);\
157}
158
159CPU_CONVERT(be, 16, uint16_t)
160CPU_CONVERT(be, 32, uint32_t)
161CPU_CONVERT(be, 64, uint64_t)
162
163CPU_CONVERT(le, 16, uint16_t)
164CPU_CONVERT(le, 32, uint32_t)
165CPU_CONVERT(le, 64, uint64_t)
166
167/* unaligned versions (optimized for frequent unaligned accesses)*/
168
169#if defined(__i386__) || defined(__powerpc__)
170
171#define cpu_to_le16wu(p, v) cpu_to_le16w(p, v)
172#define cpu_to_le32wu(p, v) cpu_to_le32w(p, v)
173#define le16_to_cpupu(p) le16_to_cpup(p)
174#define le32_to_cpupu(p) le32_to_cpup(p)
175
176#define cpu_to_be16wu(p, v) cpu_to_be16w(p, v)
177#define cpu_to_be32wu(p, v) cpu_to_be32w(p, v)
178
179#else
180
181static inline void cpu_to_le16wu(uint16_t *p, uint16_t v)
182{
183 uint8_t *p1 = (uint8_t *)p;
184
185 p1[0] = (uint8_t)v;
186 p1[1] = v >> 8;
187}
188
189static inline void cpu_to_le32wu(uint32_t *p, uint32_t v)
190{
191 uint8_t *p1 = (uint8_t *)p;
192
193 p1[0] = (uint8_t)v;
194 p1[1] = v >> 8;
195 p1[2] = v >> 16;
196 p1[3] = v >> 24;
197}
198
199static inline uint16_t le16_to_cpupu(const uint16_t *p)
200{
201 const uint8_t *p1 = (const uint8_t *)p;
202 return p1[0] | (p1[1] << 8);
203}
204
205static inline uint32_t le32_to_cpupu(const uint32_t *p)
206{
207 const uint8_t *p1 = (const uint8_t *)p;
208 return p1[0] | (p1[1] << 8) | (p1[2] << 16) | (p1[3] << 24);
209}
210
211static inline void cpu_to_be16wu(uint16_t *p, uint16_t v)
212{
213 uint8_t *p1 = (uint8_t *)p;
214
215 p1[0] = v >> 8;
216 p1[1] = (uint8_t)v;
217}
218
219static inline void cpu_to_be32wu(uint32_t *p, uint32_t v)
220{
221 uint8_t *p1 = (uint8_t *)p;
222
223 p1[0] = v >> 24;
224 p1[1] = v >> 16;
225 p1[2] = v >> 8;
226 p1[3] = (uint8_t)v;
227}
228
229#endif
230
231#ifdef WORDS_BIGENDIAN
232#define cpu_to_32wu cpu_to_be32wu
233#else
234#define cpu_to_32wu cpu_to_le32wu
235#endif
236
237#undef le_bswap
238#undef be_bswap
239#undef le_bswaps
240#undef be_bswaps
241
242#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