]>
git.proxmox.com Git - mirror_qemu.git/blob - include/qemu/bswap.h
4 #ifdef CONFIG_MACHINE_BSWAP_H
5 # include <sys/endian.h>
6 # include <machine/bswap.h>
7 #elif defined(__FreeBSD__)
8 # include <sys/endian.h>
9 #elif defined(__HAIKU__)
12 #define BSWAP_FROM_FALLBACKS
13 #endif /* ! CONFIG_MACHINE_BSWAP_H */
19 #ifdef BSWAP_FROM_FALLBACKS
21 #define bswap16(_x) __builtin_bswap16(_x)
23 #define bswap32(_x) __builtin_bswap32(_x)
25 #define bswap64(_x) __builtin_bswap64(_x)
28 #undef BSWAP_FROM_FALLBACKS
30 static inline void bswap16s(uint16_t *s
)
32 *s
= __builtin_bswap16(*s
);
35 static inline void bswap32s(uint32_t *s
)
37 *s
= __builtin_bswap32(*s
);
40 static inline void bswap64s(uint64_t *s
)
42 *s
= __builtin_bswap64(*s
);
46 #define be_bswap(v, size) (v)
47 #define le_bswap(v, size) glue(__builtin_bswap, size)(v)
48 #define be_bswaps(v, size)
49 #define le_bswaps(p, size) \
50 do { *p = glue(__builtin_bswap, size)(*p); } while (0)
52 #define le_bswap(v, size) (v)
53 #define be_bswap(v, size) glue(__builtin_bswap, size)(v)
54 #define le_bswaps(v, size)
55 #define be_bswaps(p, size) \
56 do { *p = glue(__builtin_bswap, size)(*p); } while (0)
60 * Endianness conversion functions between host cpu and specified endianness.
61 * (We list the complete set of prototypes produced by the macros below
62 * to assist people who search the headers to find their definitions.)
64 * uint16_t le16_to_cpu(uint16_t v);
65 * uint32_t le32_to_cpu(uint32_t v);
66 * uint64_t le64_to_cpu(uint64_t v);
67 * uint16_t be16_to_cpu(uint16_t v);
68 * uint32_t be32_to_cpu(uint32_t v);
69 * uint64_t be64_to_cpu(uint64_t v);
71 * Convert the value @v from the specified format to the native
72 * endianness of the host CPU by byteswapping if necessary, and
73 * return the converted value.
75 * uint16_t cpu_to_le16(uint16_t v);
76 * uint32_t cpu_to_le32(uint32_t v);
77 * uint64_t cpu_to_le64(uint64_t v);
78 * uint16_t cpu_to_be16(uint16_t v);
79 * uint32_t cpu_to_be32(uint32_t v);
80 * uint64_t cpu_to_be64(uint64_t v);
82 * Convert the value @v from the native endianness of the host CPU to
83 * the specified format by byteswapping if necessary, and return
84 * the converted value.
86 * void le16_to_cpus(uint16_t *v);
87 * void le32_to_cpus(uint32_t *v);
88 * void le64_to_cpus(uint64_t *v);
89 * void be16_to_cpus(uint16_t *v);
90 * void be32_to_cpus(uint32_t *v);
91 * void be64_to_cpus(uint64_t *v);
93 * Do an in-place conversion of the value pointed to by @v from the
94 * specified format to the native endianness of the host CPU.
96 * void cpu_to_le16s(uint16_t *v);
97 * void cpu_to_le32s(uint32_t *v);
98 * void cpu_to_le64s(uint64_t *v);
99 * void cpu_to_be16s(uint16_t *v);
100 * void cpu_to_be32s(uint32_t *v);
101 * void cpu_to_be64s(uint64_t *v);
103 * Do an in-place conversion of the value pointed to by @v from the
104 * native endianness of the host CPU to the specified format.
106 * Both X_to_cpu() and cpu_to_X() perform the same operation; you
107 * should use whichever one is better documenting of the function your
108 * code is performing.
110 * Do not use these functions for conversion of values which are in guest
111 * memory, since the data may not be sufficiently aligned for the host CPU's
112 * load and store instructions. Instead you should use the ld*_p() and
113 * st*_p() functions, which perform loads and stores of data of any
114 * required size and endianness and handle possible misalignment.
117 #define CPU_CONVERT(endian, size, type)\
118 static inline type endian ## size ## _to_cpu(type v)\
120 return glue(endian, _bswap)(v, size);\
123 static inline type cpu_to_ ## endian ## size(type v)\
125 return glue(endian, _bswap)(v, size);\
128 static inline void endian ## size ## _to_cpus(type *p)\
130 glue(endian, _bswaps)(p, size);\
133 static inline void cpu_to_ ## endian ## size ## s(type *p)\
135 glue(endian, _bswaps)(p, size);\
138 CPU_CONVERT(be
, 16, uint16_t)
139 CPU_CONVERT(be
, 32, uint32_t)
140 CPU_CONVERT(be
, 64, uint64_t)
142 CPU_CONVERT(le
, 16, uint16_t)
143 CPU_CONVERT(le
, 32, uint32_t)
144 CPU_CONVERT(le
, 64, uint64_t)
147 * Same as cpu_to_le{16,32}, except that gcc will figure the result is
148 * a compile-time constant if you pass in a constant. So this can be
149 * used to initialize static variables.
152 # define const_le32(_x) \
153 ((((_x) & 0x000000ffU) << 24) | \
154 (((_x) & 0x0000ff00U) << 8) | \
155 (((_x) & 0x00ff0000U) >> 8) | \
156 (((_x) & 0xff000000U) >> 24))
157 # define const_le16(_x) \
158 ((((_x) & 0x00ff) << 8) | \
159 (((_x) & 0xff00) >> 8))
161 # define const_le32(_x) (_x)
162 # define const_le16(_x) (_x)
165 /* unaligned/endian-independent pointer access */
168 * the generic syntax is:
170 * load: ld{type}{sign}{size}_{endian}_p(ptr)
172 * store: st{type}{size}_{endian}_p(ptr, val)
174 * Note there are small differences with the softmmu access API!
177 * (empty): integer access
181 * (empty): for 32 or 64 bit sizes (including floats and doubles)
196 * (except for byte accesses, which have no endian infix).
198 * The target endian accessors are obviously only available to source
199 * files which are built per-target; they are defined in cpu-all.h.
201 * In all cases these functions take a host pointer.
202 * For accessors that take a guest address rather than a
203 * host address, see the cpu_{ld,st}_* accessors defined in
206 * For cases where the size to be used is not fixed at compile time,
208 * stn_{endian}_p(ptr, sz, val)
209 * which stores @val to @ptr as an @endian-order number @sz bytes in size
211 * ldn_{endian}_p(ptr, sz)
212 * which loads @sz bytes from @ptr as an unsigned @endian-order number
213 * and returns it in a uint64_t.
216 static inline int ldub_p(const void *ptr
)
218 return *(uint8_t *)ptr
;
221 static inline int ldsb_p(const void *ptr
)
223 return *(int8_t *)ptr
;
226 static inline void stb_p(void *ptr
, uint8_t v
)
232 * Any compiler worth its salt will turn these memcpy into native unaligned
233 * operations. Thus we don't need to play games with packed attributes, or
234 * inline byte-by-byte stores.
235 * Some compilation environments (eg some fortify-source implementations)
236 * may intercept memcpy() in a way that defeats the compiler optimization,
237 * though, so we use __builtin_memcpy() to give ourselves the best chance
238 * of good performance.
241 static inline int lduw_he_p(const void *ptr
)
244 __builtin_memcpy(&r
, ptr
, sizeof(r
));
248 static inline int ldsw_he_p(const void *ptr
)
251 __builtin_memcpy(&r
, ptr
, sizeof(r
));
255 static inline void stw_he_p(void *ptr
, uint16_t v
)
257 __builtin_memcpy(ptr
, &v
, sizeof(v
));
260 static inline int ldl_he_p(const void *ptr
)
263 __builtin_memcpy(&r
, ptr
, sizeof(r
));
267 static inline void stl_he_p(void *ptr
, uint32_t v
)
269 __builtin_memcpy(ptr
, &v
, sizeof(v
));
272 static inline uint64_t ldq_he_p(const void *ptr
)
275 __builtin_memcpy(&r
, ptr
, sizeof(r
));
279 static inline void stq_he_p(void *ptr
, uint64_t v
)
281 __builtin_memcpy(ptr
, &v
, sizeof(v
));
284 static inline int lduw_le_p(const void *ptr
)
286 return (uint16_t)le_bswap(lduw_he_p(ptr
), 16);
289 static inline int ldsw_le_p(const void *ptr
)
291 return (int16_t)le_bswap(lduw_he_p(ptr
), 16);
294 static inline int ldl_le_p(const void *ptr
)
296 return le_bswap(ldl_he_p(ptr
), 32);
299 static inline uint64_t ldq_le_p(const void *ptr
)
301 return le_bswap(ldq_he_p(ptr
), 64);
304 static inline void stw_le_p(void *ptr
, uint16_t v
)
306 stw_he_p(ptr
, le_bswap(v
, 16));
309 static inline void stl_le_p(void *ptr
, uint32_t v
)
311 stl_he_p(ptr
, le_bswap(v
, 32));
314 static inline void stq_le_p(void *ptr
, uint64_t v
)
316 stq_he_p(ptr
, le_bswap(v
, 64));
319 static inline int lduw_be_p(const void *ptr
)
321 return (uint16_t)be_bswap(lduw_he_p(ptr
), 16);
324 static inline int ldsw_be_p(const void *ptr
)
326 return (int16_t)be_bswap(lduw_he_p(ptr
), 16);
329 static inline int ldl_be_p(const void *ptr
)
331 return be_bswap(ldl_he_p(ptr
), 32);
334 static inline uint64_t ldq_be_p(const void *ptr
)
336 return be_bswap(ldq_he_p(ptr
), 64);
339 static inline void stw_be_p(void *ptr
, uint16_t v
)
341 stw_he_p(ptr
, be_bswap(v
, 16));
344 static inline void stl_be_p(void *ptr
, uint32_t v
)
346 stl_he_p(ptr
, be_bswap(v
, 32));
349 static inline void stq_be_p(void *ptr
, uint64_t v
)
351 stq_he_p(ptr
, be_bswap(v
, 64));
354 static inline unsigned long leul_to_cpu(unsigned long v
)
356 #if HOST_LONG_BITS == 32
357 return le_bswap(v
, 32);
358 #elif HOST_LONG_BITS == 64
359 return le_bswap(v
, 64);
361 # error Unknown sizeof long
365 /* Store v to p as a sz byte value in host order */
366 #define DO_STN_LDN_P(END) \
367 static inline void stn_## END ## _p(void *ptr, int sz, uint64_t v) \
374 stw_ ## END ## _p(ptr, v); \
377 stl_ ## END ## _p(ptr, v); \
380 stq_ ## END ## _p(ptr, v); \
383 g_assert_not_reached(); \
386 static inline uint64_t ldn_## END ## _p(const void *ptr, int sz) \
390 return ldub_p(ptr); \
392 return lduw_ ## END ## _p(ptr); \
394 return (uint32_t)ldl_ ## END ## _p(ptr); \
396 return ldq_ ## END ## _p(ptr); \
398 g_assert_not_reached(); \