]>
git.proxmox.com Git - mirror_qemu.git/blob - include/qemu/bswap.h
4 #include "config-host.h"
7 #include "fpu/softfloat.h"
9 #ifdef CONFIG_MACHINE_BSWAP_H
10 # include <sys/endian.h>
11 # include <sys/types.h>
12 # include <machine/bswap.h>
13 #elif defined(CONFIG_BYTESWAP_H)
14 # include <byteswap.h>
16 static inline uint16_t bswap16(uint16_t x
)
21 static inline uint32_t bswap32(uint32_t x
)
26 static inline uint64_t bswap64(uint64_t x
)
31 static inline uint16_t bswap16(uint16_t x
)
33 return (((x
& 0x00ff) << 8) |
37 static inline uint32_t bswap32(uint32_t x
)
39 return (((x
& 0x000000ffU
) << 24) |
40 ((x
& 0x0000ff00U
) << 8) |
41 ((x
& 0x00ff0000U
) >> 8) |
42 ((x
& 0xff000000U
) >> 24));
45 static inline uint64_t bswap64(uint64_t x
)
47 return (((x
& 0x00000000000000ffULL
) << 56) |
48 ((x
& 0x000000000000ff00ULL
) << 40) |
49 ((x
& 0x0000000000ff0000ULL
) << 24) |
50 ((x
& 0x00000000ff000000ULL
) << 8) |
51 ((x
& 0x000000ff00000000ULL
) >> 8) |
52 ((x
& 0x0000ff0000000000ULL
) >> 24) |
53 ((x
& 0x00ff000000000000ULL
) >> 40) |
54 ((x
& 0xff00000000000000ULL
) >> 56));
56 #endif /* ! CONFIG_MACHINE_BSWAP_H */
58 static inline void bswap16s(uint16_t *s
)
63 static inline void bswap32s(uint32_t *s
)
68 static inline void bswap64s(uint64_t *s
)
73 #if defined(HOST_WORDS_BIGENDIAN)
74 #define be_bswap(v, size) (v)
75 #define le_bswap(v, size) glue(bswap, size)(v)
76 #define be_bswaps(v, size)
77 #define le_bswaps(p, size) do { *p = glue(bswap, size)(*p); } while(0)
79 #define le_bswap(v, size) (v)
80 #define be_bswap(v, size) glue(bswap, size)(v)
81 #define le_bswaps(v, size)
82 #define be_bswaps(p, size) do { *p = glue(bswap, size)(*p); } while(0)
85 #define CPU_CONVERT(endian, size, type)\
86 static inline type endian ## size ## _to_cpu(type v)\
88 return glue(endian, _bswap)(v, size);\
91 static inline type cpu_to_ ## endian ## size(type v)\
93 return glue(endian, _bswap)(v, size);\
96 static inline void endian ## size ## _to_cpus(type *p)\
98 glue(endian, _bswaps)(p, size);\
101 static inline void cpu_to_ ## endian ## size ## s(type *p)\
103 glue(endian, _bswaps)(p, size);\
106 static inline type endian ## size ## _to_cpup(const type *p)\
108 return glue(glue(endian, size), _to_cpu)(*p);\
111 static inline void cpu_to_ ## endian ## size ## w(type *p, type v)\
113 *p = glue(glue(cpu_to_, endian), size)(v);\
116 CPU_CONVERT(be
, 16, uint16_t)
117 CPU_CONVERT(be
, 32, uint32_t)
118 CPU_CONVERT(be
, 64, uint64_t)
120 CPU_CONVERT(le
, 16, uint16_t)
121 CPU_CONVERT(le
, 32, uint32_t)
122 CPU_CONVERT(le
, 64, uint64_t)
124 /* len must be one of 1, 2, 4 */
125 static inline uint32_t qemu_bswap_len(uint32_t value
, int len
)
127 return bswap32(value
) >> (32 - 8 * len
);
130 /* Unions for reinterpreting between floats and integers. */
139 #if defined(HOST_WORDS_BIGENDIAN)
163 #if defined(HOST_WORDS_BIGENDIAN)
188 /* unaligned/endian-independent pointer access */
191 * the generic syntax is:
193 * load: ld{type}{sign}{size}{endian}_p(ptr)
195 * store: st{type}{size}{endian}_p(ptr, val)
197 * Note there are small differences with the softmmu access API!
200 * (empty): integer access
204 * (empty): for floats or 32 bit size
215 * (empty): host endian
220 static inline int ldub_p(const void *ptr
)
222 return *(uint8_t *)ptr
;
225 static inline int ldsb_p(const void *ptr
)
227 return *(int8_t *)ptr
;
230 static inline void stb_p(void *ptr
, int v
)
235 /* Any compiler worth its salt will turn these memcpy into native unaligned
236 operations. Thus we don't need to play games with packed attributes, or
237 inline byte-by-byte stores. */
239 static inline int lduw_p(const void *ptr
)
242 memcpy(&r
, ptr
, sizeof(r
));
246 static inline int ldsw_p(const void *ptr
)
249 memcpy(&r
, ptr
, sizeof(r
));
253 static inline void stw_p(void *ptr
, uint16_t v
)
255 memcpy(ptr
, &v
, sizeof(v
));
258 static inline int ldl_p(const void *ptr
)
261 memcpy(&r
, ptr
, sizeof(r
));
265 static inline void stl_p(void *ptr
, uint32_t v
)
267 memcpy(ptr
, &v
, sizeof(v
));
270 static inline uint64_t ldq_p(const void *ptr
)
273 memcpy(&r
, ptr
, sizeof(r
));
277 static inline void stq_p(void *ptr
, uint64_t v
)
279 memcpy(ptr
, &v
, sizeof(v
));
282 static inline int lduw_le_p(const void *ptr
)
284 return (uint16_t)le_bswap(lduw_p(ptr
), 16);
287 static inline int ldsw_le_p(const void *ptr
)
289 return (int16_t)le_bswap(lduw_p(ptr
), 16);
292 static inline int ldl_le_p(const void *ptr
)
294 return le_bswap(ldl_p(ptr
), 32);
297 static inline uint64_t ldq_le_p(const void *ptr
)
299 return le_bswap(ldq_p(ptr
), 64);
302 static inline void stw_le_p(void *ptr
, int v
)
304 stw_p(ptr
, le_bswap(v
, 16));
307 static inline void stl_le_p(void *ptr
, int v
)
309 stl_p(ptr
, le_bswap(v
, 32));
312 static inline void stq_le_p(void *ptr
, uint64_t v
)
314 stq_p(ptr
, le_bswap(v
, 64));
319 static inline float32
ldfl_le_p(const void *ptr
)
326 static inline void stfl_le_p(void *ptr
, float32 v
)
333 static inline float64
ldfq_le_p(const void *ptr
)
336 u
.ll
= ldq_le_p(ptr
);
340 static inline void stfq_le_p(void *ptr
, float64 v
)
347 static inline int lduw_be_p(const void *ptr
)
349 return (uint16_t)be_bswap(lduw_p(ptr
), 16);
352 static inline int ldsw_be_p(const void *ptr
)
354 return (int16_t)be_bswap(lduw_p(ptr
), 16);
357 static inline int ldl_be_p(const void *ptr
)
359 return be_bswap(ldl_p(ptr
), 32);
362 static inline uint64_t ldq_be_p(const void *ptr
)
364 return be_bswap(ldq_p(ptr
), 64);
367 static inline void stw_be_p(void *ptr
, int v
)
369 stw_p(ptr
, be_bswap(v
, 16));
372 static inline void stl_be_p(void *ptr
, int v
)
374 stl_p(ptr
, be_bswap(v
, 32));
377 static inline void stq_be_p(void *ptr
, uint64_t v
)
379 stq_p(ptr
, be_bswap(v
, 64));
384 static inline float32
ldfl_be_p(const void *ptr
)
391 static inline void stfl_be_p(void *ptr
, float32 v
)
398 static inline float64
ldfq_be_p(const void *ptr
)
401 u
.ll
= ldq_be_p(ptr
);
405 static inline void stfq_be_p(void *ptr
, float64 v
)
412 /* Legacy unaligned versions. Note that we never had a complete set. */
414 static inline void cpu_to_le16wu(uint16_t *p
, uint16_t v
)
419 static inline void cpu_to_le32wu(uint32_t *p
, uint32_t v
)
424 static inline uint16_t le16_to_cpupu(const uint16_t *p
)
429 static inline uint32_t le32_to_cpupu(const uint32_t *p
)
434 static inline uint32_t be32_to_cpupu(const uint32_t *p
)
439 static inline void cpu_to_be16wu(uint16_t *p
, uint16_t v
)
444 static inline void cpu_to_be32wu(uint32_t *p
, uint32_t v
)
449 static inline void cpu_to_be64wu(uint64_t *p
, uint64_t v
)
454 static inline void cpu_to_32wu(uint32_t *p
, uint32_t v
)
459 static inline unsigned long leul_to_cpu(unsigned long v
)
461 /* In order to break an include loop between here and
462 qemu-common.h, don't rely on HOST_LONG_BITS. */
463 #if ULONG_MAX == UINT32_MAX
464 return le_bswap(v
, 32);
465 #elif ULONG_MAX == UINT64_MAX
466 return le_bswap(v
, 64);
468 # error Unknown sizeof long