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(CONFIG_BYTESWAP_H)
15 # include <byteswap.h>
17 static inline uint16_t bswap16(uint16_t x)
22 static inline uint32_t bswap32(uint32_t x)
27 static inline uint64_t bswap64(uint64_t x)
32 static inline uint16_t bswap16(uint16_t x)
34 return (((x & 0x00ff) << 8) |
38 static inline uint32_t bswap32(uint32_t x)
40 return (((x & 0x000000ffU) << 24) |
41 ((x & 0x0000ff00U) << 8) |
42 ((x & 0x00ff0000U) >> 8) |
43 ((x & 0xff000000U) >> 24));
46 static inline uint64_t bswap64(uint64_t x)
48 return (((x & 0x00000000000000ffULL) << 56) |
49 ((x & 0x000000000000ff00ULL) << 40) |
50 ((x & 0x0000000000ff0000ULL) << 24) |
51 ((x & 0x00000000ff000000ULL) << 8) |
52 ((x & 0x000000ff00000000ULL) >> 8) |
53 ((x & 0x0000ff0000000000ULL) >> 24) |
54 ((x & 0x00ff000000000000ULL) >> 40) |
55 ((x & 0xff00000000000000ULL) >> 56));
57 #endif /* ! CONFIG_MACHINE_BSWAP_H */
59 static inline void bswap16s(uint16_t *s)
64 static inline void bswap32s(uint32_t *s)
69 static inline void bswap64s(uint64_t *s)
74 #if defined(HOST_WORDS_BIGENDIAN)
75 #define be_bswap(v, size) (v)
76 #define le_bswap(v, size) glue(bswap, size)(v)
77 #define be_bswaps(v, size)
78 #define le_bswaps(p, size) do { *p = glue(bswap, size)(*p); } while(0)
80 #define le_bswap(v, size) (v)
81 #define be_bswap(v, size) glue(bswap, size)(v)
82 #define le_bswaps(v, size)
83 #define be_bswaps(p, size) do { *p = glue(bswap, size)(*p); } while(0)
86 #define CPU_CONVERT(endian, size, type)\
87 static inline type endian ## size ## _to_cpu(type v)\
89 return glue(endian, _bswap)(v, size);\
92 static inline type cpu_to_ ## endian ## size(type v)\
94 return glue(endian, _bswap)(v, size);\
97 static inline void endian ## size ## _to_cpus(type *p)\
99 glue(endian, _bswaps)(p, size);\
102 static inline void cpu_to_ ## endian ## size ## s(type *p)\
104 glue(endian, _bswaps)(p, size);\
107 static inline type endian ## size ## _to_cpup(const type *p)\
109 return glue(glue(endian, size), _to_cpu)(*p);\
112 static inline void cpu_to_ ## endian ## size ## w(type *p, type v)\
114 *p = glue(glue(cpu_to_, endian), size)(v);\
117 CPU_CONVERT(be, 16, uint16_t)
118 CPU_CONVERT(be, 32, uint32_t)
119 CPU_CONVERT(be, 64, uint64_t)
121 CPU_CONVERT(le, 16, uint16_t)
122 CPU_CONVERT(le, 32, uint32_t)
123 CPU_CONVERT(le, 64, uint64_t)
125 /* len must be one of 1, 2, 4 */
126 static inline uint32_t qemu_bswap_len(uint32_t value, int len)
128 return bswap32(value) >> (32 - 8 * len);
131 /* Unions for reinterpreting between floats and integers. */
140 #if defined(HOST_WORDS_BIGENDIAN)
164 #if defined(HOST_WORDS_BIGENDIAN)
189 /* unaligned/endian-independent pointer access */
192 * the generic syntax is:
194 * load: ld{type}{sign}{size}{endian}_p(ptr)
196 * store: st{type}{size}{endian}_p(ptr, val)
198 * Note there are small differences with the softmmu access API!
201 * (empty): integer access
205 * (empty): for floats or 32 bit size
216 * (empty): host endian
221 static inline int ldub_p(const void *ptr)
223 return *(uint8_t *)ptr;
226 static inline int ldsb_p(const void *ptr)
228 return *(int8_t *)ptr;
231 static inline void stb_p(void *ptr, uint8_t v)
236 /* Any compiler worth its salt will turn these memcpy into native unaligned
237 operations. Thus we don't need to play games with packed attributes, or
238 inline byte-by-byte stores. */
240 static inline int lduw_p(const void *ptr)
243 memcpy(&r, ptr, sizeof(r));
247 static inline int ldsw_p(const void *ptr)
250 memcpy(&r, ptr, sizeof(r));
254 static inline void stw_p(void *ptr, uint16_t v)
256 memcpy(ptr, &v, sizeof(v));
259 static inline int ldl_p(const void *ptr)
262 memcpy(&r, ptr, sizeof(r));
266 static inline void stl_p(void *ptr, uint32_t v)
268 memcpy(ptr, &v, sizeof(v));
271 static inline uint64_t ldq_p(const void *ptr)
274 memcpy(&r, ptr, sizeof(r));
278 static inline void stq_p(void *ptr, uint64_t v)
280 memcpy(ptr, &v, sizeof(v));
283 static inline int lduw_le_p(const void *ptr)
285 return (uint16_t)le_bswap(lduw_p(ptr), 16);
288 static inline int ldsw_le_p(const void *ptr)
290 return (int16_t)le_bswap(lduw_p(ptr), 16);
293 static inline int ldl_le_p(const void *ptr)
295 return le_bswap(ldl_p(ptr), 32);
298 static inline uint64_t ldq_le_p(const void *ptr)
300 return le_bswap(ldq_p(ptr), 64);
303 static inline void stw_le_p(void *ptr, uint16_t v)
305 stw_p(ptr, le_bswap(v, 16));
308 static inline void stl_le_p(void *ptr, uint32_t v)
310 stl_p(ptr, le_bswap(v, 32));
313 static inline void stq_le_p(void *ptr, uint64_t v)
315 stq_p(ptr, le_bswap(v, 64));
320 static inline float32 ldfl_le_p(const void *ptr)
327 static inline void stfl_le_p(void *ptr, float32 v)
334 static inline float64 ldfq_le_p(const void *ptr)
337 u.ll = ldq_le_p(ptr);
341 static inline void stfq_le_p(void *ptr, float64 v)
348 static inline int lduw_be_p(const void *ptr)
350 return (uint16_t)be_bswap(lduw_p(ptr), 16);
353 static inline int ldsw_be_p(const void *ptr)
355 return (int16_t)be_bswap(lduw_p(ptr), 16);
358 static inline int ldl_be_p(const void *ptr)
360 return be_bswap(ldl_p(ptr), 32);
363 static inline uint64_t ldq_be_p(const void *ptr)
365 return be_bswap(ldq_p(ptr), 64);
368 static inline void stw_be_p(void *ptr, uint16_t v)
370 stw_p(ptr, be_bswap(v, 16));
373 static inline void stl_be_p(void *ptr, uint32_t v)
375 stl_p(ptr, be_bswap(v, 32));
378 static inline void stq_be_p(void *ptr, uint64_t v)
380 stq_p(ptr, be_bswap(v, 64));
385 static inline float32 ldfl_be_p(const void *ptr)
392 static inline void stfl_be_p(void *ptr, float32 v)
399 static inline float64 ldfq_be_p(const void *ptr)
402 u.ll = ldq_be_p(ptr);
406 static inline void stfq_be_p(void *ptr, float64 v)
413 static inline unsigned long leul_to_cpu(unsigned long v)
415 /* In order to break an include loop between here and
416 qemu-common.h, don't rely on HOST_LONG_BITS. */
417 #if ULONG_MAX == UINT32_MAX
418 return le_bswap(v, 32);
419 #elif ULONG_MAX == UINT64_MAX
420 return le_bswap(v, 64);
422 # error Unknown sizeof long