]>
Commit | Line | Data |
---|---|---|
ab93bbe2 FB |
1 | #ifndef BSWAP_H |
2 | #define BSWAP_H | |
3 | ||
4 | #include "config-host.h" | |
ab93bbe2 | 5 | #include <inttypes.h> |
91107fdf | 6 | #include <limits.h> |
ea44910e | 7 | #include <string.h> |
6b4c305c | 8 | #include "fpu/softfloat.h" |
ab93bbe2 | 9 | |
5735147e | 10 | #ifdef CONFIG_MACHINE_BSWAP_H |
cdfe2851 RH |
11 | # include <sys/endian.h> |
12 | # include <sys/types.h> | |
13 | # include <machine/bswap.h> | |
de03c316 AF |
14 | #elif defined(__FreeBSD__) |
15 | # include <sys/endian.h> | |
cdfe2851 RH |
16 | #elif defined(CONFIG_BYTESWAP_H) |
17 | # include <byteswap.h> | |
ab93bbe2 | 18 | |
ab93bbe2 FB |
19 | static inline uint16_t bswap16(uint16_t x) |
20 | { | |
21 | return bswap_16(x); | |
22 | } | |
23 | ||
5fafdf24 | 24 | static inline uint32_t bswap32(uint32_t x) |
ab93bbe2 FB |
25 | { |
26 | return bswap_32(x); | |
27 | } | |
28 | ||
5fafdf24 | 29 | static inline uint64_t bswap64(uint64_t x) |
ab93bbe2 FB |
30 | { |
31 | return bswap_64(x); | |
32 | } | |
cdfe2851 RH |
33 | # else |
34 | static inline uint16_t bswap16(uint16_t x) | |
35 | { | |
36 | return (((x & 0x00ff) << 8) | | |
37 | ((x & 0xff00) >> 8)); | |
38 | } | |
ab93bbe2 | 39 | |
cdfe2851 RH |
40 | static inline uint32_t bswap32(uint32_t x) |
41 | { | |
42 | return (((x & 0x000000ffU) << 24) | | |
43 | ((x & 0x0000ff00U) << 8) | | |
44 | ((x & 0x00ff0000U) >> 8) | | |
45 | ((x & 0xff000000U) >> 24)); | |
46 | } | |
47 | ||
48 | static inline uint64_t bswap64(uint64_t x) | |
49 | { | |
50 | return (((x & 0x00000000000000ffULL) << 56) | | |
51 | ((x & 0x000000000000ff00ULL) << 40) | | |
52 | ((x & 0x0000000000ff0000ULL) << 24) | | |
53 | ((x & 0x00000000ff000000ULL) << 8) | | |
54 | ((x & 0x000000ff00000000ULL) >> 8) | | |
55 | ((x & 0x0000ff0000000000ULL) >> 24) | | |
56 | ((x & 0x00ff000000000000ULL) >> 40) | | |
57 | ((x & 0xff00000000000000ULL) >> 56)); | |
58 | } | |
5735147e | 59 | #endif /* ! CONFIG_MACHINE_BSWAP_H */ |
1360677c | 60 | |
ab93bbe2 FB |
61 | static inline void bswap16s(uint16_t *s) |
62 | { | |
63 | *s = bswap16(*s); | |
64 | } | |
65 | ||
66 | static inline void bswap32s(uint32_t *s) | |
67 | { | |
68 | *s = bswap32(*s); | |
69 | } | |
70 | ||
71 | static inline void bswap64s(uint64_t *s) | |
72 | { | |
73 | *s = bswap64(*s); | |
74 | } | |
75 | ||
e2542fe2 | 76 | #if defined(HOST_WORDS_BIGENDIAN) |
af8ffdfd | 77 | #define be_bswap(v, size) (v) |
a4cbfe24 | 78 | #define le_bswap(v, size) glue(bswap, size)(v) |
af8ffdfd | 79 | #define be_bswaps(v, size) |
a4cbfe24 | 80 | #define le_bswaps(p, size) do { *p = glue(bswap, size)(*p); } while(0) |
af8ffdfd FB |
81 | #else |
82 | #define le_bswap(v, size) (v) | |
a4cbfe24 | 83 | #define be_bswap(v, size) glue(bswap, size)(v) |
af8ffdfd | 84 | #define le_bswaps(v, size) |
a4cbfe24 | 85 | #define be_bswaps(p, size) do { *p = glue(bswap, size)(*p); } while(0) |
af8ffdfd FB |
86 | #endif |
87 | ||
88 | #define CPU_CONVERT(endian, size, type)\ | |
89 | static inline type endian ## size ## _to_cpu(type v)\ | |
90 | {\ | |
a4cbfe24 | 91 | return glue(endian, _bswap)(v, size);\ |
af8ffdfd FB |
92 | }\ |
93 | \ | |
94 | static inline type cpu_to_ ## endian ## size(type v)\ | |
95 | {\ | |
a4cbfe24 | 96 | return glue(endian, _bswap)(v, size);\ |
af8ffdfd FB |
97 | }\ |
98 | \ | |
99 | static inline void endian ## size ## _to_cpus(type *p)\ | |
100 | {\ | |
a4cbfe24 | 101 | glue(endian, _bswaps)(p, size);\ |
af8ffdfd FB |
102 | }\ |
103 | \ | |
104 | static inline void cpu_to_ ## endian ## size ## s(type *p)\ | |
105 | {\ | |
a4cbfe24 | 106 | glue(endian, _bswaps)(p, size);\ |
af8ffdfd FB |
107 | }\ |
108 | \ | |
109 | static inline type endian ## size ## _to_cpup(const type *p)\ | |
110 | {\ | |
a4cbfe24 | 111 | return glue(glue(endian, size), _to_cpu)(*p);\ |
af8ffdfd FB |
112 | }\ |
113 | \ | |
114 | static inline void cpu_to_ ## endian ## size ## w(type *p, type v)\ | |
115 | {\ | |
a4cbfe24 | 116 | *p = glue(glue(cpu_to_, endian), size)(v);\ |
af8ffdfd FB |
117 | } |
118 | ||
119 | CPU_CONVERT(be, 16, uint16_t) | |
120 | CPU_CONVERT(be, 32, uint32_t) | |
121 | CPU_CONVERT(be, 64, uint64_t) | |
122 | ||
123 | CPU_CONVERT(le, 16, uint16_t) | |
124 | CPU_CONVERT(le, 32, uint32_t) | |
125 | CPU_CONVERT(le, 64, uint64_t) | |
126 | ||
e73d6e3a MT |
127 | /* len must be one of 1, 2, 4 */ |
128 | static inline uint32_t qemu_bswap_len(uint32_t value, int len) | |
129 | { | |
130 | return bswap32(value) >> (32 - 8 * len); | |
131 | } | |
132 | ||
7db2145a RH |
133 | /* Unions for reinterpreting between floats and integers. */ |
134 | ||
cbbab922 PB |
135 | typedef union { |
136 | float32 f; | |
137 | uint32_t l; | |
138 | } CPU_FloatU; | |
139 | ||
140 | typedef union { | |
141 | float64 d; | |
142 | #if defined(HOST_WORDS_BIGENDIAN) | |
143 | struct { | |
144 | uint32_t upper; | |
145 | uint32_t lower; | |
146 | } l; | |
147 | #else | |
148 | struct { | |
149 | uint32_t lower; | |
150 | uint32_t upper; | |
151 | } l; | |
152 | #endif | |
153 | uint64_t ll; | |
154 | } CPU_DoubleU; | |
155 | ||
156 | typedef union { | |
157 | floatx80 d; | |
158 | struct { | |
159 | uint64_t lower; | |
160 | uint16_t upper; | |
161 | } l; | |
162 | } CPU_LDoubleU; | |
163 | ||
164 | typedef union { | |
165 | float128 q; | |
166 | #if defined(HOST_WORDS_BIGENDIAN) | |
167 | struct { | |
168 | uint32_t upmost; | |
169 | uint32_t upper; | |
170 | uint32_t lower; | |
171 | uint32_t lowest; | |
172 | } l; | |
173 | struct { | |
174 | uint64_t upper; | |
175 | uint64_t lower; | |
176 | } ll; | |
177 | #else | |
178 | struct { | |
179 | uint32_t lowest; | |
180 | uint32_t lower; | |
181 | uint32_t upper; | |
182 | uint32_t upmost; | |
183 | } l; | |
184 | struct { | |
185 | uint64_t lower; | |
186 | uint64_t upper; | |
187 | } ll; | |
188 | #endif | |
189 | } CPU_QuadU; | |
190 | ||
191 | /* unaligned/endian-independent pointer access */ | |
192 | ||
193 | /* | |
194 | * the generic syntax is: | |
195 | * | |
196 | * load: ld{type}{sign}{size}{endian}_p(ptr) | |
197 | * | |
198 | * store: st{type}{size}{endian}_p(ptr, val) | |
199 | * | |
200 | * Note there are small differences with the softmmu access API! | |
201 | * | |
202 | * type is: | |
203 | * (empty): integer access | |
204 | * f : float access | |
205 | * | |
206 | * sign is: | |
207 | * (empty): for floats or 32 bit size | |
208 | * u : unsigned | |
209 | * s : signed | |
210 | * | |
211 | * size is: | |
212 | * b: 8 bits | |
213 | * w: 16 bits | |
214 | * l: 32 bits | |
215 | * q: 64 bits | |
216 | * | |
217 | * endian is: | |
1a3de8db | 218 | * he : host endian |
cbbab922 PB |
219 | * be : big endian |
220 | * le : little endian | |
1a3de8db | 221 | * (except for byte accesses, which have no endian infix). |
cbbab922 | 222 | */ |
c732a52d | 223 | |
cbbab922 PB |
224 | static inline int ldub_p(const void *ptr) |
225 | { | |
226 | return *(uint8_t *)ptr; | |
227 | } | |
228 | ||
229 | static inline int ldsb_p(const void *ptr) | |
230 | { | |
231 | return *(int8_t *)ptr; | |
232 | } | |
233 | ||
0064aceb | 234 | static inline void stb_p(void *ptr, uint8_t v) |
cbbab922 PB |
235 | { |
236 | *(uint8_t *)ptr = v; | |
237 | } | |
238 | ||
7db2145a RH |
239 | /* Any compiler worth its salt will turn these memcpy into native unaligned |
240 | operations. Thus we don't need to play games with packed attributes, or | |
241 | inline byte-by-byte stores. */ | |
242 | ||
1a3de8db | 243 | static inline int lduw_he_p(const void *ptr) |
7db2145a RH |
244 | { |
245 | uint16_t r; | |
246 | memcpy(&r, ptr, sizeof(r)); | |
247 | return r; | |
248 | } | |
249 | ||
1a3de8db | 250 | static inline int ldsw_he_p(const void *ptr) |
7db2145a RH |
251 | { |
252 | int16_t r; | |
253 | memcpy(&r, ptr, sizeof(r)); | |
254 | return r; | |
255 | } | |
256 | ||
1a3de8db | 257 | static inline void stw_he_p(void *ptr, uint16_t v) |
7db2145a RH |
258 | { |
259 | memcpy(ptr, &v, sizeof(v)); | |
260 | } | |
261 | ||
1a3de8db | 262 | static inline int ldl_he_p(const void *ptr) |
7db2145a RH |
263 | { |
264 | int32_t r; | |
265 | memcpy(&r, ptr, sizeof(r)); | |
266 | return r; | |
267 | } | |
268 | ||
1a3de8db | 269 | static inline void stl_he_p(void *ptr, uint32_t v) |
7db2145a RH |
270 | { |
271 | memcpy(ptr, &v, sizeof(v)); | |
272 | } | |
273 | ||
1a3de8db | 274 | static inline uint64_t ldq_he_p(const void *ptr) |
7db2145a RH |
275 | { |
276 | uint64_t r; | |
277 | memcpy(&r, ptr, sizeof(r)); | |
278 | return r; | |
279 | } | |
280 | ||
1a3de8db | 281 | static inline void stq_he_p(void *ptr, uint64_t v) |
7db2145a RH |
282 | { |
283 | memcpy(ptr, &v, sizeof(v)); | |
284 | } | |
285 | ||
cbbab922 PB |
286 | static inline int lduw_le_p(const void *ptr) |
287 | { | |
1a3de8db | 288 | return (uint16_t)le_bswap(lduw_he_p(ptr), 16); |
cbbab922 PB |
289 | } |
290 | ||
291 | static inline int ldsw_le_p(const void *ptr) | |
292 | { | |
1a3de8db | 293 | return (int16_t)le_bswap(lduw_he_p(ptr), 16); |
cbbab922 PB |
294 | } |
295 | ||
296 | static inline int ldl_le_p(const void *ptr) | |
297 | { | |
1a3de8db | 298 | return le_bswap(ldl_he_p(ptr), 32); |
cbbab922 PB |
299 | } |
300 | ||
301 | static inline uint64_t ldq_le_p(const void *ptr) | |
302 | { | |
1a3de8db | 303 | return le_bswap(ldq_he_p(ptr), 64); |
cbbab922 PB |
304 | } |
305 | ||
55e7c29e | 306 | static inline void stw_le_p(void *ptr, uint16_t v) |
cbbab922 | 307 | { |
1a3de8db | 308 | stw_he_p(ptr, le_bswap(v, 16)); |
cbbab922 PB |
309 | } |
310 | ||
55e7c29e | 311 | static inline void stl_le_p(void *ptr, uint32_t v) |
cbbab922 | 312 | { |
1a3de8db | 313 | stl_he_p(ptr, le_bswap(v, 32)); |
cbbab922 PB |
314 | } |
315 | ||
316 | static inline void stq_le_p(void *ptr, uint64_t v) | |
317 | { | |
1a3de8db | 318 | stq_he_p(ptr, le_bswap(v, 64)); |
cbbab922 PB |
319 | } |
320 | ||
321 | /* float access */ | |
322 | ||
323 | static inline float32 ldfl_le_p(const void *ptr) | |
324 | { | |
612d590e RH |
325 | CPU_FloatU u; |
326 | u.l = ldl_le_p(ptr); | |
cbbab922 PB |
327 | return u.f; |
328 | } | |
329 | ||
330 | static inline void stfl_le_p(void *ptr, float32 v) | |
331 | { | |
612d590e | 332 | CPU_FloatU u; |
cbbab922 | 333 | u.f = v; |
612d590e | 334 | stl_le_p(ptr, u.l); |
cbbab922 PB |
335 | } |
336 | ||
337 | static inline float64 ldfq_le_p(const void *ptr) | |
338 | { | |
339 | CPU_DoubleU u; | |
612d590e | 340 | u.ll = ldq_le_p(ptr); |
cbbab922 PB |
341 | return u.d; |
342 | } | |
343 | ||
344 | static inline void stfq_le_p(void *ptr, float64 v) | |
345 | { | |
346 | CPU_DoubleU u; | |
347 | u.d = v; | |
612d590e | 348 | stq_le_p(ptr, u.ll); |
cbbab922 PB |
349 | } |
350 | ||
cbbab922 PB |
351 | static inline int lduw_be_p(const void *ptr) |
352 | { | |
1a3de8db | 353 | return (uint16_t)be_bswap(lduw_he_p(ptr), 16); |
cbbab922 PB |
354 | } |
355 | ||
356 | static inline int ldsw_be_p(const void *ptr) | |
357 | { | |
1a3de8db | 358 | return (int16_t)be_bswap(lduw_he_p(ptr), 16); |
cbbab922 PB |
359 | } |
360 | ||
361 | static inline int ldl_be_p(const void *ptr) | |
362 | { | |
1a3de8db | 363 | return be_bswap(ldl_he_p(ptr), 32); |
cbbab922 PB |
364 | } |
365 | ||
366 | static inline uint64_t ldq_be_p(const void *ptr) | |
367 | { | |
1a3de8db | 368 | return be_bswap(ldq_he_p(ptr), 64); |
cbbab922 PB |
369 | } |
370 | ||
55e7c29e | 371 | static inline void stw_be_p(void *ptr, uint16_t v) |
cbbab922 | 372 | { |
1a3de8db | 373 | stw_he_p(ptr, be_bswap(v, 16)); |
cbbab922 PB |
374 | } |
375 | ||
55e7c29e | 376 | static inline void stl_be_p(void *ptr, uint32_t v) |
cbbab922 | 377 | { |
1a3de8db | 378 | stl_he_p(ptr, be_bswap(v, 32)); |
cbbab922 PB |
379 | } |
380 | ||
381 | static inline void stq_be_p(void *ptr, uint64_t v) | |
382 | { | |
1a3de8db | 383 | stq_he_p(ptr, be_bswap(v, 64)); |
cbbab922 PB |
384 | } |
385 | ||
386 | /* float access */ | |
387 | ||
388 | static inline float32 ldfl_be_p(const void *ptr) | |
389 | { | |
612d590e RH |
390 | CPU_FloatU u; |
391 | u.l = ldl_be_p(ptr); | |
cbbab922 PB |
392 | return u.f; |
393 | } | |
394 | ||
395 | static inline void stfl_be_p(void *ptr, float32 v) | |
396 | { | |
612d590e | 397 | CPU_FloatU u; |
cbbab922 | 398 | u.f = v; |
612d590e | 399 | stl_be_p(ptr, u.l); |
cbbab922 PB |
400 | } |
401 | ||
402 | static inline float64 ldfq_be_p(const void *ptr) | |
403 | { | |
404 | CPU_DoubleU u; | |
612d590e | 405 | u.ll = ldq_be_p(ptr); |
cbbab922 PB |
406 | return u.d; |
407 | } | |
408 | ||
409 | static inline void stfq_be_p(void *ptr, float64 v) | |
410 | { | |
411 | CPU_DoubleU u; | |
412 | u.d = v; | |
612d590e | 413 | stq_be_p(ptr, u.ll); |
cbbab922 PB |
414 | } |
415 | ||
c732a52d RH |
416 | static inline unsigned long leul_to_cpu(unsigned long v) |
417 | { | |
91107fdf RH |
418 | /* In order to break an include loop between here and |
419 | qemu-common.h, don't rely on HOST_LONG_BITS. */ | |
420 | #if ULONG_MAX == UINT32_MAX | |
421 | return le_bswap(v, 32); | |
422 | #elif ULONG_MAX == UINT64_MAX | |
423 | return le_bswap(v, 64); | |
424 | #else | |
425 | # error Unknown sizeof long | |
426 | #endif | |
c732a52d RH |
427 | } |
428 | ||
612d590e RH |
429 | #undef le_bswap |
430 | #undef be_bswap | |
431 | #undef le_bswaps | |
432 | #undef be_bswaps | |
cbbab922 | 433 | |
ab93bbe2 | 434 | #endif /* BSWAP_H */ |