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