4 #include "config-host.h"
8 #include "fpu/softfloat.h"
10 #ifdef CONFIG_MACHINE_BSWAP_H
11 # include <sys/endian.h>
12 # include <sys/types.h>
13 # include <machine/bswap.h>
14 #elif defined(__FreeBSD__)
15 # include <sys/endian.h>
16 #elif defined(CONFIG_BYTESWAP_H)
17 # include <byteswap.h>
19 static inline uint16_t bswap16(uint16_t x
)
24 static inline uint32_t bswap32(uint32_t x
)
29 static inline uint64_t bswap64(uint64_t x
)
34 static inline uint16_t bswap16(uint16_t x
)
36 return (((x
& 0x00ff) << 8) |
40 static inline uint32_t bswap32(uint32_t x
)
42 return (((x
& 0x000000ffU
) << 24) |
43 ((x
& 0x0000ff00U
) << 8) |
44 ((x
& 0x00ff0000U
) >> 8) |
45 ((x
& 0xff000000U
) >> 24));
48 static inline uint64_t bswap64(uint64_t x
)
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));
59 #endif /* ! CONFIG_MACHINE_BSWAP_H */
61 static inline void bswap16s(uint16_t *s
)
66 static inline void bswap32s(uint32_t *s
)
71 static inline void bswap64s(uint64_t *s
)
76 #if defined(HOST_WORDS_BIGENDIAN)
77 #define be_bswap(v, size) (v)
78 #define le_bswap(v, size) glue(bswap, size)(v)
79 #define be_bswaps(v, size)
80 #define le_bswaps(p, size) do { *p = glue(bswap, size)(*p); } while(0)
82 #define le_bswap(v, size) (v)
83 #define be_bswap(v, size) glue(bswap, size)(v)
84 #define le_bswaps(v, size)
85 #define be_bswaps(p, size) do { *p = glue(bswap, size)(*p); } while(0)
88 #define CPU_CONVERT(endian, size, type)\
89 static inline type endian ## size ## _to_cpu(type v)\
91 return glue(endian, _bswap)(v, size);\
94 static inline type cpu_to_ ## endian ## size(type v)\
96 return glue(endian, _bswap)(v, size);\
99 static inline void endian ## size ## _to_cpus(type *p)\
101 glue(endian, _bswaps)(p, size);\
104 static inline void cpu_to_ ## endian ## size ## s(type *p)\
106 glue(endian, _bswaps)(p, size);\
109 static inline type endian ## size ## _to_cpup(const type *p)\
111 return glue(glue(endian, size), _to_cpu)(*p);\
114 static inline void cpu_to_ ## endian ## size ## w(type *p, type v)\
116 *p = glue(glue(cpu_to_, endian), size)(v);\
119 CPU_CONVERT(be
, 16, uint16_t)
120 CPU_CONVERT(be
, 32, uint32_t)
121 CPU_CONVERT(be
, 64, uint64_t)
123 CPU_CONVERT(le
, 16, uint16_t)
124 CPU_CONVERT(le
, 32, uint32_t)
125 CPU_CONVERT(le
, 64, uint64_t)
127 /* len must be one of 1, 2, 4 */
128 static inline uint32_t qemu_bswap_len(uint32_t value
, int len
)
130 return bswap32(value
) >> (32 - 8 * len
);
133 /* Unions for reinterpreting between floats and integers. */
142 #if defined(HOST_WORDS_BIGENDIAN)
166 #if defined(HOST_WORDS_BIGENDIAN)
191 /* unaligned/endian-independent pointer access */
194 * the generic syntax is:
196 * load: ld{type}{sign}{size}{endian}_p(ptr)
198 * store: st{type}{size}{endian}_p(ptr, val)
200 * Note there are small differences with the softmmu access API!
203 * (empty): integer access
207 * (empty): for floats or 32 bit size
221 * (except for byte accesses, which have no endian infix).
224 static inline int ldub_p(const void *ptr
)
226 return *(uint8_t *)ptr
;
229 static inline int ldsb_p(const void *ptr
)
231 return *(int8_t *)ptr
;
234 static inline void stb_p(void *ptr
, uint8_t v
)
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. */
243 static inline int lduw_he_p(const void *ptr
)
246 memcpy(&r
, ptr
, sizeof(r
));
250 static inline int ldsw_he_p(const void *ptr
)
253 memcpy(&r
, ptr
, sizeof(r
));
257 static inline void stw_he_p(void *ptr
, uint16_t v
)
259 memcpy(ptr
, &v
, sizeof(v
));
262 static inline int ldl_he_p(const void *ptr
)
265 memcpy(&r
, ptr
, sizeof(r
));
269 static inline void stl_he_p(void *ptr
, uint32_t v
)
271 memcpy(ptr
, &v
, sizeof(v
));
274 static inline uint64_t ldq_he_p(const void *ptr
)
277 memcpy(&r
, ptr
, sizeof(r
));
281 static inline void stq_he_p(void *ptr
, uint64_t v
)
283 memcpy(ptr
, &v
, sizeof(v
));
286 static inline int lduw_le_p(const void *ptr
)
288 return (uint16_t)le_bswap(lduw_he_p(ptr
), 16);
291 static inline int ldsw_le_p(const void *ptr
)
293 return (int16_t)le_bswap(lduw_he_p(ptr
), 16);
296 static inline int ldl_le_p(const void *ptr
)
298 return le_bswap(ldl_he_p(ptr
), 32);
301 static inline uint64_t ldq_le_p(const void *ptr
)
303 return le_bswap(ldq_he_p(ptr
), 64);
306 static inline void stw_le_p(void *ptr
, uint16_t v
)
308 stw_he_p(ptr
, le_bswap(v
, 16));
311 static inline void stl_le_p(void *ptr
, uint32_t v
)
313 stl_he_p(ptr
, le_bswap(v
, 32));
316 static inline void stq_le_p(void *ptr
, uint64_t v
)
318 stq_he_p(ptr
, le_bswap(v
, 64));
323 static inline float32
ldfl_le_p(const void *ptr
)
330 static inline void stfl_le_p(void *ptr
, float32 v
)
337 static inline float64
ldfq_le_p(const void *ptr
)
340 u
.ll
= ldq_le_p(ptr
);
344 static inline void stfq_le_p(void *ptr
, float64 v
)
351 static inline int lduw_be_p(const void *ptr
)
353 return (uint16_t)be_bswap(lduw_he_p(ptr
), 16);
356 static inline int ldsw_be_p(const void *ptr
)
358 return (int16_t)be_bswap(lduw_he_p(ptr
), 16);
361 static inline int ldl_be_p(const void *ptr
)
363 return be_bswap(ldl_he_p(ptr
), 32);
366 static inline uint64_t ldq_be_p(const void *ptr
)
368 return be_bswap(ldq_he_p(ptr
), 64);
371 static inline void stw_be_p(void *ptr
, uint16_t v
)
373 stw_he_p(ptr
, be_bswap(v
, 16));
376 static inline void stl_be_p(void *ptr
, uint32_t v
)
378 stl_he_p(ptr
, be_bswap(v
, 32));
381 static inline void stq_be_p(void *ptr
, uint64_t v
)
383 stq_he_p(ptr
, be_bswap(v
, 64));
388 static inline float32
ldfl_be_p(const void *ptr
)
395 static inline void stfl_be_p(void *ptr
, float32 v
)
402 static inline float64
ldfq_be_p(const void *ptr
)
405 u
.ll
= ldq_be_p(ptr
);
409 static inline void stfq_be_p(void *ptr
, float64 v
)
416 static inline unsigned long leul_to_cpu(unsigned long v
)
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);
425 # error Unknown sizeof long