xref: /openbmc/qemu/include/exec/tswap.h (revision f8ed3648)
1 /*
2  * Macros for swapping a value if the endianness is different
3  * between the target and the host.
4  *
5  * SPDX-License-Identifier: LGPL-2.1-or-later
6  */
7 
8 #ifndef TSWAP_H
9 #define TSWAP_H
10 
11 #include "hw/core/cpu.h"
12 #include "qemu/bswap.h"
13 
14 /*
15  * If we're in target-specific code, we can hard-code the swapping
16  * condition, otherwise we have to do (slower) run-time checks.
17  */
18 #ifdef NEED_CPU_H
19 #define target_needs_bswap()  (HOST_BIG_ENDIAN != TARGET_BIG_ENDIAN)
20 #else
21 #define target_needs_bswap()  (target_words_bigendian() != HOST_BIG_ENDIAN)
22 #endif
23 
24 static inline uint16_t tswap16(uint16_t s)
25 {
26     if (target_needs_bswap()) {
27         return bswap16(s);
28     } else {
29         return s;
30     }
31 }
32 
33 static inline uint32_t tswap32(uint32_t s)
34 {
35     if (target_needs_bswap()) {
36         return bswap32(s);
37     } else {
38         return s;
39     }
40 }
41 
42 static inline uint64_t tswap64(uint64_t s)
43 {
44     if (target_needs_bswap()) {
45         return bswap64(s);
46     } else {
47         return s;
48     }
49 }
50 
51 static inline void tswap16s(uint16_t *s)
52 {
53     if (target_needs_bswap()) {
54         *s = bswap16(*s);
55     }
56 }
57 
58 static inline void tswap32s(uint32_t *s)
59 {
60     if (target_needs_bswap()) {
61         *s = bswap32(*s);
62     }
63 }
64 
65 static inline void tswap64s(uint64_t *s)
66 {
67     if (target_needs_bswap()) {
68         *s = bswap64(*s);
69     }
70 }
71 
72 #endif  /* TSWAP_H */
73