4 #include "fpu/softfloat.h"
6 #ifdef CONFIG_MACHINE_BSWAP_H
7 # include <sys/endian.h>
8 # include <machine/bswap.h>
9 #elif defined(__FreeBSD__)
10 # include <sys/endian.h>
11 #elif defined(CONFIG_BYTESWAP_H)
12 # include <byteswap.h>
14 static inline uint16_t bswap16(uint16_t x
)
19 static inline uint32_t bswap32(uint32_t x
)
24 static inline uint64_t bswap64(uint64_t x
)
29 static inline uint16_t bswap16(uint16_t x
)
31 return (((x
& 0x00ff) << 8) |
35 static inline uint32_t bswap32(uint32_t x
)
37 return (((x
& 0x000000ffU
) << 24) |
38 ((x
& 0x0000ff00U
) << 8) |
39 ((x
& 0x00ff0000U
) >> 8) |
40 ((x
& 0xff000000U
) >> 24));
43 static inline uint64_t bswap64(uint64_t x
)
45 return (((x
& 0x00000000000000ffULL
) << 56) |
46 ((x
& 0x000000000000ff00ULL
) << 40) |
47 ((x
& 0x0000000000ff0000ULL
) << 24) |
48 ((x
& 0x00000000ff000000ULL
) << 8) |
49 ((x
& 0x000000ff00000000ULL
) >> 8) |
50 ((x
& 0x0000ff0000000000ULL
) >> 24) |
51 ((x
& 0x00ff000000000000ULL
) >> 40) |
52 ((x
& 0xff00000000000000ULL
) >> 56));
54 #endif /* ! CONFIG_MACHINE_BSWAP_H */
56 static inline void bswap16s(uint16_t *s
)
61 static inline void bswap32s(uint32_t *s
)
66 static inline void bswap64s(uint64_t *s
)
71 #if defined(HOST_WORDS_BIGENDIAN)
72 #define be_bswap(v, size) (v)
73 #define le_bswap(v, size) glue(bswap, size)(v)
74 #define be_bswaps(v, size)
75 #define le_bswaps(p, size) do { *p = glue(bswap, size)(*p); } while(0)
77 #define le_bswap(v, size) (v)
78 #define be_bswap(v, size) glue(bswap, size)(v)
79 #define le_bswaps(v, size)
80 #define be_bswaps(p, size) do { *p = glue(bswap, size)(*p); } while(0)
83 #define CPU_CONVERT(endian, size, type)\
84 static inline type endian ## size ## _to_cpu(type v)\
86 return glue(endian, _bswap)(v, size);\
89 static inline type cpu_to_ ## endian ## size(type v)\
91 return glue(endian, _bswap)(v, size);\
94 static inline void endian ## size ## _to_cpus(type *p)\
96 glue(endian, _bswaps)(p, size);\
99 static inline void cpu_to_ ## endian ## size ## s(type *p)\
101 glue(endian, _bswaps)(p, size);\
104 static inline type endian ## size ## _to_cpup(const type *p)\
106 return glue(glue(endian, size), _to_cpu)(*p);\
109 static inline void cpu_to_ ## endian ## size ## w(type *p, type v)\
111 *p = glue(glue(cpu_to_, endian), size)(v);\
114 CPU_CONVERT(be
, 16, uint16_t)
115 CPU_CONVERT(be
, 32, uint32_t)
116 CPU_CONVERT(be
, 64, uint64_t)
118 CPU_CONVERT(le
, 16, uint16_t)
119 CPU_CONVERT(le
, 32, uint32_t)
120 CPU_CONVERT(le
, 64, uint64_t)
122 /* len must be one of 1, 2, 4 */
123 static inline uint32_t qemu_bswap_len(uint32_t value
, int len
)
125 return bswap32(value
) >> (32 - 8 * len
);
129 * Same as cpu_to_le{16,23}, except that gcc will figure the result is
130 * a compile-time constant if you pass in a constant. So this can be
131 * used to initialize static variables.
133 #if defined(HOST_WORDS_BIGENDIAN)
134 # define const_le32(_x) \
135 ((((_x) & 0x000000ffU) << 24) | \
136 (((_x) & 0x0000ff00U) << 8) | \
137 (((_x) & 0x00ff0000U) >> 8) | \
138 (((_x) & 0xff000000U) >> 24))
139 # define const_le16(_x) \
140 ((((_x) & 0x00ff) << 8) | \
141 (((_x) & 0xff00) >> 8))
143 # define const_le32(_x) (_x)
144 # define const_le16(_x) (_x)
147 /* Unions for reinterpreting between floats and integers. */
156 #if defined(HOST_WORDS_BIGENDIAN)
180 #if defined(HOST_WORDS_BIGENDIAN)
205 /* unaligned/endian-independent pointer access */
208 * the generic syntax is:
210 * load: ld{type}{sign}{size}{endian}_p(ptr)
212 * store: st{type}{size}{endian}_p(ptr, val)
214 * Note there are small differences with the softmmu access API!
217 * (empty): integer access
221 * (empty): for 32 or 64 bit sizes (including floats and doubles)
236 * (except for byte accesses, which have no endian infix).
238 * The target endian accessors are obviously only available to source
239 * files which are built per-target; they are defined in cpu-all.h.
241 * In all cases these functions take a host pointer.
242 * For accessors that take a guest address rather than a
243 * host address, see the cpu_{ld,st}_* accessors defined in
247 static inline int ldub_p(const void *ptr
)
249 return *(uint8_t *)ptr
;
252 static inline int ldsb_p(const void *ptr
)
254 return *(int8_t *)ptr
;
257 static inline void stb_p(void *ptr
, uint8_t v
)
262 /* Any compiler worth its salt will turn these memcpy into native unaligned
263 operations. Thus we don't need to play games with packed attributes, or
264 inline byte-by-byte stores. */
266 static inline int lduw_he_p(const void *ptr
)
269 memcpy(&r
, ptr
, sizeof(r
));
273 static inline int ldsw_he_p(const void *ptr
)
276 memcpy(&r
, ptr
, sizeof(r
));
280 static inline void stw_he_p(void *ptr
, uint16_t v
)
282 memcpy(ptr
, &v
, sizeof(v
));
285 static inline int ldl_he_p(const void *ptr
)
288 memcpy(&r
, ptr
, sizeof(r
));
292 static inline void stl_he_p(void *ptr
, uint32_t v
)
294 memcpy(ptr
, &v
, sizeof(v
));
297 static inline uint64_t ldq_he_p(const void *ptr
)
300 memcpy(&r
, ptr
, sizeof(r
));
304 static inline void stq_he_p(void *ptr
, uint64_t v
)
306 memcpy(ptr
, &v
, sizeof(v
));
309 static inline int lduw_le_p(const void *ptr
)
311 return (uint16_t)le_bswap(lduw_he_p(ptr
), 16);
314 static inline int ldsw_le_p(const void *ptr
)
316 return (int16_t)le_bswap(lduw_he_p(ptr
), 16);
319 static inline int ldl_le_p(const void *ptr
)
321 return le_bswap(ldl_he_p(ptr
), 32);
324 static inline uint64_t ldq_le_p(const void *ptr
)
326 return le_bswap(ldq_he_p(ptr
), 64);
329 static inline void stw_le_p(void *ptr
, uint16_t v
)
331 stw_he_p(ptr
, le_bswap(v
, 16));
334 static inline void stl_le_p(void *ptr
, uint32_t v
)
336 stl_he_p(ptr
, le_bswap(v
, 32));
339 static inline void stq_le_p(void *ptr
, uint64_t v
)
341 stq_he_p(ptr
, le_bswap(v
, 64));
346 static inline float32
ldfl_le_p(const void *ptr
)
353 static inline void stfl_le_p(void *ptr
, float32 v
)
360 static inline float64
ldfq_le_p(const void *ptr
)
363 u
.ll
= ldq_le_p(ptr
);
367 static inline void stfq_le_p(void *ptr
, float64 v
)
374 static inline int lduw_be_p(const void *ptr
)
376 return (uint16_t)be_bswap(lduw_he_p(ptr
), 16);
379 static inline int ldsw_be_p(const void *ptr
)
381 return (int16_t)be_bswap(lduw_he_p(ptr
), 16);
384 static inline int ldl_be_p(const void *ptr
)
386 return be_bswap(ldl_he_p(ptr
), 32);
389 static inline uint64_t ldq_be_p(const void *ptr
)
391 return be_bswap(ldq_he_p(ptr
), 64);
394 static inline void stw_be_p(void *ptr
, uint16_t v
)
396 stw_he_p(ptr
, be_bswap(v
, 16));
399 static inline void stl_be_p(void *ptr
, uint32_t v
)
401 stl_he_p(ptr
, be_bswap(v
, 32));
404 static inline void stq_be_p(void *ptr
, uint64_t v
)
406 stq_he_p(ptr
, be_bswap(v
, 64));
411 static inline float32
ldfl_be_p(const void *ptr
)
418 static inline void stfl_be_p(void *ptr
, float32 v
)
425 static inline float64
ldfq_be_p(const void *ptr
)
428 u
.ll
= ldq_be_p(ptr
);
432 static inline void stfq_be_p(void *ptr
, float64 v
)
439 static inline unsigned long leul_to_cpu(unsigned long v
)
441 #if HOST_LONG_BITS == 32
442 return le_bswap(v
, 32);
443 #elif HOST_LONG_BITS == 64
444 return le_bswap(v
, 64);
446 # error Unknown sizeof long