]>
git.proxmox.com Git - mirror_qemu.git/blob - include/qemu/bswap.h
ca2b4c3f1556f87bc2145da3a79afa358e2cf7be
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__)
11 #elif defined(CONFIG_BYTESWAP_H)
12 # include <byteswap.h>
13 #define BSWAP_FROM_BYTESWAP
15 #define BSWAP_FROM_FALLBACKS
16 #endif /* ! CONFIG_MACHINE_BSWAP_H */
22 #ifdef BSWAP_FROM_BYTESWAP
23 static inline uint16_t bswap16(uint16_t x
)
28 static inline uint32_t bswap32(uint32_t x
)
33 static inline uint64_t bswap64(uint64_t x
)
39 #ifdef BSWAP_FROM_FALLBACKS
41 #define bswap16(_x) __builtin_bswap16(_x)
43 #define bswap32(_x) __builtin_bswap32(_x)
45 #define bswap64(_x) __builtin_bswap64(_x)
48 #undef BSWAP_FROM_BYTESWAP
49 #undef BSWAP_FROM_FALLBACKS
51 static inline void bswap16s(uint16_t *s
)
56 static inline void bswap32s(uint32_t *s
)
61 static inline void bswap64s(uint64_t *s
)
67 #define be_bswap(v, size) (v)
68 #define le_bswap(v, size) glue(bswap, size)(v)
69 #define be_bswaps(v, size)
70 #define le_bswaps(p, size) do { *p = glue(bswap, size)(*p); } while(0)
72 #define le_bswap(v, size) (v)
73 #define be_bswap(v, size) glue(bswap, size)(v)
74 #define le_bswaps(v, size)
75 #define be_bswaps(p, size) do { *p = glue(bswap, size)(*p); } while(0)
79 * Endianness conversion functions between host cpu and specified endianness.
80 * (We list the complete set of prototypes produced by the macros below
81 * to assist people who search the headers to find their definitions.)
83 * uint16_t le16_to_cpu(uint16_t v);
84 * uint32_t le32_to_cpu(uint32_t v);
85 * uint64_t le64_to_cpu(uint64_t v);
86 * uint16_t be16_to_cpu(uint16_t v);
87 * uint32_t be32_to_cpu(uint32_t v);
88 * uint64_t be64_to_cpu(uint64_t v);
90 * Convert the value @v from the specified format to the native
91 * endianness of the host CPU by byteswapping if necessary, and
92 * return the converted value.
94 * uint16_t cpu_to_le16(uint16_t v);
95 * uint32_t cpu_to_le32(uint32_t v);
96 * uint64_t cpu_to_le64(uint64_t v);
97 * uint16_t cpu_to_be16(uint16_t v);
98 * uint32_t cpu_to_be32(uint32_t v);
99 * uint64_t cpu_to_be64(uint64_t v);
101 * Convert the value @v from the native endianness of the host CPU to
102 * the specified format by byteswapping if necessary, and return
103 * the converted value.
105 * void le16_to_cpus(uint16_t *v);
106 * void le32_to_cpus(uint32_t *v);
107 * void le64_to_cpus(uint64_t *v);
108 * void be16_to_cpus(uint16_t *v);
109 * void be32_to_cpus(uint32_t *v);
110 * void be64_to_cpus(uint64_t *v);
112 * Do an in-place conversion of the value pointed to by @v from the
113 * specified format to the native endianness of the host CPU.
115 * void cpu_to_le16s(uint16_t *v);
116 * void cpu_to_le32s(uint32_t *v);
117 * void cpu_to_le64s(uint64_t *v);
118 * void cpu_to_be16s(uint16_t *v);
119 * void cpu_to_be32s(uint32_t *v);
120 * void cpu_to_be64s(uint64_t *v);
122 * Do an in-place conversion of the value pointed to by @v from the
123 * native endianness of the host CPU to the specified format.
125 * Both X_to_cpu() and cpu_to_X() perform the same operation; you
126 * should use whichever one is better documenting of the function your
127 * code is performing.
129 * Do not use these functions for conversion of values which are in guest
130 * memory, since the data may not be sufficiently aligned for the host CPU's
131 * load and store instructions. Instead you should use the ld*_p() and
132 * st*_p() functions, which perform loads and stores of data of any
133 * required size and endianness and handle possible misalignment.
136 #define CPU_CONVERT(endian, size, type)\
137 static inline type endian ## size ## _to_cpu(type v)\
139 return glue(endian, _bswap)(v, size);\
142 static inline type cpu_to_ ## endian ## size(type v)\
144 return glue(endian, _bswap)(v, size);\
147 static inline void endian ## size ## _to_cpus(type *p)\
149 glue(endian, _bswaps)(p, size);\
152 static inline void cpu_to_ ## endian ## size ## s(type *p)\
154 glue(endian, _bswaps)(p, size);\
157 CPU_CONVERT(be
, 16, uint16_t)
158 CPU_CONVERT(be
, 32, uint32_t)
159 CPU_CONVERT(be
, 64, uint64_t)
161 CPU_CONVERT(le
, 16, uint16_t)
162 CPU_CONVERT(le
, 32, uint32_t)
163 CPU_CONVERT(le
, 64, uint64_t)
166 * Same as cpu_to_le{16,32}, except that gcc will figure the result is
167 * a compile-time constant if you pass in a constant. So this can be
168 * used to initialize static variables.
171 # define const_le32(_x) \
172 ((((_x) & 0x000000ffU) << 24) | \
173 (((_x) & 0x0000ff00U) << 8) | \
174 (((_x) & 0x00ff0000U) >> 8) | \
175 (((_x) & 0xff000000U) >> 24))
176 # define const_le16(_x) \
177 ((((_x) & 0x00ff) << 8) | \
178 (((_x) & 0xff00) >> 8))
180 # define const_le32(_x) (_x)
181 # define const_le16(_x) (_x)
184 /* unaligned/endian-independent pointer access */
187 * the generic syntax is:
189 * load: ld{type}{sign}{size}_{endian}_p(ptr)
191 * store: st{type}{size}_{endian}_p(ptr, val)
193 * Note there are small differences with the softmmu access API!
196 * (empty): integer access
200 * (empty): for 32 or 64 bit sizes (including floats and doubles)
215 * (except for byte accesses, which have no endian infix).
217 * The target endian accessors are obviously only available to source
218 * files which are built per-target; they are defined in cpu-all.h.
220 * In all cases these functions take a host pointer.
221 * For accessors that take a guest address rather than a
222 * host address, see the cpu_{ld,st}_* accessors defined in
225 * For cases where the size to be used is not fixed at compile time,
227 * stn_{endian}_p(ptr, sz, val)
228 * which stores @val to @ptr as an @endian-order number @sz bytes in size
230 * ldn_{endian}_p(ptr, sz)
231 * which loads @sz bytes from @ptr as an unsigned @endian-order number
232 * and returns it in a uint64_t.
235 static inline int ldub_p(const void *ptr
)
237 return *(uint8_t *)ptr
;
240 static inline int ldsb_p(const void *ptr
)
242 return *(int8_t *)ptr
;
245 static inline void stb_p(void *ptr
, uint8_t v
)
251 * Any compiler worth its salt will turn these memcpy into native unaligned
252 * operations. Thus we don't need to play games with packed attributes, or
253 * inline byte-by-byte stores.
254 * Some compilation environments (eg some fortify-source implementations)
255 * may intercept memcpy() in a way that defeats the compiler optimization,
256 * though, so we use __builtin_memcpy() to give ourselves the best chance
257 * of good performance.
260 static inline int lduw_he_p(const void *ptr
)
263 __builtin_memcpy(&r
, ptr
, sizeof(r
));
267 static inline int ldsw_he_p(const void *ptr
)
270 __builtin_memcpy(&r
, ptr
, sizeof(r
));
274 static inline void stw_he_p(void *ptr
, uint16_t v
)
276 __builtin_memcpy(ptr
, &v
, sizeof(v
));
279 static inline int ldl_he_p(const void *ptr
)
282 __builtin_memcpy(&r
, ptr
, sizeof(r
));
286 static inline void stl_he_p(void *ptr
, uint32_t v
)
288 __builtin_memcpy(ptr
, &v
, sizeof(v
));
291 static inline uint64_t ldq_he_p(const void *ptr
)
294 __builtin_memcpy(&r
, ptr
, sizeof(r
));
298 static inline void stq_he_p(void *ptr
, uint64_t v
)
300 __builtin_memcpy(ptr
, &v
, sizeof(v
));
303 static inline int lduw_le_p(const void *ptr
)
305 return (uint16_t)le_bswap(lduw_he_p(ptr
), 16);
308 static inline int ldsw_le_p(const void *ptr
)
310 return (int16_t)le_bswap(lduw_he_p(ptr
), 16);
313 static inline int ldl_le_p(const void *ptr
)
315 return le_bswap(ldl_he_p(ptr
), 32);
318 static inline uint64_t ldq_le_p(const void *ptr
)
320 return le_bswap(ldq_he_p(ptr
), 64);
323 static inline void stw_le_p(void *ptr
, uint16_t v
)
325 stw_he_p(ptr
, le_bswap(v
, 16));
328 static inline void stl_le_p(void *ptr
, uint32_t v
)
330 stl_he_p(ptr
, le_bswap(v
, 32));
333 static inline void stq_le_p(void *ptr
, uint64_t v
)
335 stq_he_p(ptr
, le_bswap(v
, 64));
338 static inline int lduw_be_p(const void *ptr
)
340 return (uint16_t)be_bswap(lduw_he_p(ptr
), 16);
343 static inline int ldsw_be_p(const void *ptr
)
345 return (int16_t)be_bswap(lduw_he_p(ptr
), 16);
348 static inline int ldl_be_p(const void *ptr
)
350 return be_bswap(ldl_he_p(ptr
), 32);
353 static inline uint64_t ldq_be_p(const void *ptr
)
355 return be_bswap(ldq_he_p(ptr
), 64);
358 static inline void stw_be_p(void *ptr
, uint16_t v
)
360 stw_he_p(ptr
, be_bswap(v
, 16));
363 static inline void stl_be_p(void *ptr
, uint32_t v
)
365 stl_he_p(ptr
, be_bswap(v
, 32));
368 static inline void stq_be_p(void *ptr
, uint64_t v
)
370 stq_he_p(ptr
, be_bswap(v
, 64));
373 static inline unsigned long leul_to_cpu(unsigned long v
)
375 #if HOST_LONG_BITS == 32
376 return le_bswap(v
, 32);
377 #elif HOST_LONG_BITS == 64
378 return le_bswap(v
, 64);
380 # error Unknown sizeof long
384 /* Store v to p as a sz byte value in host order */
385 #define DO_STN_LDN_P(END) \
386 static inline void stn_## END ## _p(void *ptr, int sz, uint64_t v) \
393 stw_ ## END ## _p(ptr, v); \
396 stl_ ## END ## _p(ptr, v); \
399 stq_ ## END ## _p(ptr, v); \
402 g_assert_not_reached(); \
405 static inline uint64_t ldn_## END ## _p(const void *ptr, int sz) \
409 return ldub_p(ptr); \
411 return lduw_ ## END ## _p(ptr); \
413 return (uint32_t)ldl_ ## END ## _p(ptr); \
415 return ldq_ ## END ## _p(ptr); \
417 g_assert_not_reached(); \