]> git.proxmox.com Git - mirror_qemu.git/blob - include/qemu/bswap.h
qemu/bswap: Remove <byteswap.h> dependency
[mirror_qemu.git] / include / qemu / bswap.h
1 #ifndef BSWAP_H
2 #define BSWAP_H
3
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__)
10 # include <endian.h>
11 # else
12 #define BSWAP_FROM_FALLBACKS
13 #endif /* ! CONFIG_MACHINE_BSWAP_H */
14
15 #ifdef __cplusplus
16 extern "C" {
17 #endif
18
19 #ifdef BSWAP_FROM_FALLBACKS
20 #undef bswap16
21 #define bswap16(_x) __builtin_bswap16(_x)
22 #undef bswap32
23 #define bswap32(_x) __builtin_bswap32(_x)
24 #undef bswap64
25 #define bswap64(_x) __builtin_bswap64(_x)
26 #endif
27
28 #undef BSWAP_FROM_FALLBACKS
29
30 static inline void bswap16s(uint16_t *s)
31 {
32 *s = __builtin_bswap16(*s);
33 }
34
35 static inline void bswap32s(uint32_t *s)
36 {
37 *s = __builtin_bswap32(*s);
38 }
39
40 static inline void bswap64s(uint64_t *s)
41 {
42 *s = __builtin_bswap64(*s);
43 }
44
45 #if HOST_BIG_ENDIAN
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)
51 #else
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)
57 #endif
58
59 /**
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.)
63 *
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);
70 *
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.
74 *
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);
81 *
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.
85 *
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);
92 *
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.
95 *
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);
102 *
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.
105 *
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.
109 *
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.
115 */
116
117 #define CPU_CONVERT(endian, size, type)\
118 static inline type endian ## size ## _to_cpu(type v)\
119 {\
120 return glue(endian, _bswap)(v, size);\
121 }\
122 \
123 static inline type cpu_to_ ## endian ## size(type v)\
124 {\
125 return glue(endian, _bswap)(v, size);\
126 }\
127 \
128 static inline void endian ## size ## _to_cpus(type *p)\
129 {\
130 glue(endian, _bswaps)(p, size);\
131 }\
132 \
133 static inline void cpu_to_ ## endian ## size ## s(type *p)\
134 {\
135 glue(endian, _bswaps)(p, size);\
136 }
137
138 CPU_CONVERT(be, 16, uint16_t)
139 CPU_CONVERT(be, 32, uint32_t)
140 CPU_CONVERT(be, 64, uint64_t)
141
142 CPU_CONVERT(le, 16, uint16_t)
143 CPU_CONVERT(le, 32, uint32_t)
144 CPU_CONVERT(le, 64, uint64_t)
145
146 /*
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.
150 */
151 #if HOST_BIG_ENDIAN
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))
160 #else
161 # define const_le32(_x) (_x)
162 # define const_le16(_x) (_x)
163 #endif
164
165 /* unaligned/endian-independent pointer access */
166
167 /*
168 * the generic syntax is:
169 *
170 * load: ld{type}{sign}{size}_{endian}_p(ptr)
171 *
172 * store: st{type}{size}_{endian}_p(ptr, val)
173 *
174 * Note there are small differences with the softmmu access API!
175 *
176 * type is:
177 * (empty): integer access
178 * f : float access
179 *
180 * sign is:
181 * (empty): for 32 or 64 bit sizes (including floats and doubles)
182 * u : unsigned
183 * s : signed
184 *
185 * size is:
186 * b: 8 bits
187 * w: 16 bits
188 * l: 32 bits
189 * q: 64 bits
190 *
191 * endian is:
192 * he : host endian
193 * be : big endian
194 * le : little endian
195 * te : target endian
196 * (except for byte accesses, which have no endian infix).
197 *
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.
200 *
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
204 * cpu_ldst.h.
205 *
206 * For cases where the size to be used is not fixed at compile time,
207 * there are
208 * stn_{endian}_p(ptr, sz, val)
209 * which stores @val to @ptr as an @endian-order number @sz bytes in size
210 * and
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.
214 */
215
216 static inline int ldub_p(const void *ptr)
217 {
218 return *(uint8_t *)ptr;
219 }
220
221 static inline int ldsb_p(const void *ptr)
222 {
223 return *(int8_t *)ptr;
224 }
225
226 static inline void stb_p(void *ptr, uint8_t v)
227 {
228 *(uint8_t *)ptr = v;
229 }
230
231 /*
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.
239 */
240
241 static inline int lduw_he_p(const void *ptr)
242 {
243 uint16_t r;
244 __builtin_memcpy(&r, ptr, sizeof(r));
245 return r;
246 }
247
248 static inline int ldsw_he_p(const void *ptr)
249 {
250 int16_t r;
251 __builtin_memcpy(&r, ptr, sizeof(r));
252 return r;
253 }
254
255 static inline void stw_he_p(void *ptr, uint16_t v)
256 {
257 __builtin_memcpy(ptr, &v, sizeof(v));
258 }
259
260 static inline int ldl_he_p(const void *ptr)
261 {
262 int32_t r;
263 __builtin_memcpy(&r, ptr, sizeof(r));
264 return r;
265 }
266
267 static inline void stl_he_p(void *ptr, uint32_t v)
268 {
269 __builtin_memcpy(ptr, &v, sizeof(v));
270 }
271
272 static inline uint64_t ldq_he_p(const void *ptr)
273 {
274 uint64_t r;
275 __builtin_memcpy(&r, ptr, sizeof(r));
276 return r;
277 }
278
279 static inline void stq_he_p(void *ptr, uint64_t v)
280 {
281 __builtin_memcpy(ptr, &v, sizeof(v));
282 }
283
284 static inline int lduw_le_p(const void *ptr)
285 {
286 return (uint16_t)le_bswap(lduw_he_p(ptr), 16);
287 }
288
289 static inline int ldsw_le_p(const void *ptr)
290 {
291 return (int16_t)le_bswap(lduw_he_p(ptr), 16);
292 }
293
294 static inline int ldl_le_p(const void *ptr)
295 {
296 return le_bswap(ldl_he_p(ptr), 32);
297 }
298
299 static inline uint64_t ldq_le_p(const void *ptr)
300 {
301 return le_bswap(ldq_he_p(ptr), 64);
302 }
303
304 static inline void stw_le_p(void *ptr, uint16_t v)
305 {
306 stw_he_p(ptr, le_bswap(v, 16));
307 }
308
309 static inline void stl_le_p(void *ptr, uint32_t v)
310 {
311 stl_he_p(ptr, le_bswap(v, 32));
312 }
313
314 static inline void stq_le_p(void *ptr, uint64_t v)
315 {
316 stq_he_p(ptr, le_bswap(v, 64));
317 }
318
319 static inline int lduw_be_p(const void *ptr)
320 {
321 return (uint16_t)be_bswap(lduw_he_p(ptr), 16);
322 }
323
324 static inline int ldsw_be_p(const void *ptr)
325 {
326 return (int16_t)be_bswap(lduw_he_p(ptr), 16);
327 }
328
329 static inline int ldl_be_p(const void *ptr)
330 {
331 return be_bswap(ldl_he_p(ptr), 32);
332 }
333
334 static inline uint64_t ldq_be_p(const void *ptr)
335 {
336 return be_bswap(ldq_he_p(ptr), 64);
337 }
338
339 static inline void stw_be_p(void *ptr, uint16_t v)
340 {
341 stw_he_p(ptr, be_bswap(v, 16));
342 }
343
344 static inline void stl_be_p(void *ptr, uint32_t v)
345 {
346 stl_he_p(ptr, be_bswap(v, 32));
347 }
348
349 static inline void stq_be_p(void *ptr, uint64_t v)
350 {
351 stq_he_p(ptr, be_bswap(v, 64));
352 }
353
354 static inline unsigned long leul_to_cpu(unsigned long v)
355 {
356 #if HOST_LONG_BITS == 32
357 return le_bswap(v, 32);
358 #elif HOST_LONG_BITS == 64
359 return le_bswap(v, 64);
360 #else
361 # error Unknown sizeof long
362 #endif
363 }
364
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) \
368 { \
369 switch (sz) { \
370 case 1: \
371 stb_p(ptr, v); \
372 break; \
373 case 2: \
374 stw_ ## END ## _p(ptr, v); \
375 break; \
376 case 4: \
377 stl_ ## END ## _p(ptr, v); \
378 break; \
379 case 8: \
380 stq_ ## END ## _p(ptr, v); \
381 break; \
382 default: \
383 g_assert_not_reached(); \
384 } \
385 } \
386 static inline uint64_t ldn_## END ## _p(const void *ptr, int sz) \
387 { \
388 switch (sz) { \
389 case 1: \
390 return ldub_p(ptr); \
391 case 2: \
392 return lduw_ ## END ## _p(ptr); \
393 case 4: \
394 return (uint32_t)ldl_ ## END ## _p(ptr); \
395 case 8: \
396 return ldq_ ## END ## _p(ptr); \
397 default: \
398 g_assert_not_reached(); \
399 } \
400 }
401
402 DO_STN_LDN_P(he)
403 DO_STN_LDN_P(le)
404 DO_STN_LDN_P(be)
405
406 #undef DO_STN_LDN_P
407
408 #undef le_bswap
409 #undef be_bswap
410 #undef le_bswaps
411 #undef be_bswaps
412
413 #ifdef __cplusplus
414 }
415 #endif
416
417 #endif /* BSWAP_H */