124be3369SThomas Huth /* 224be3369SThomas Huth * Macros for swapping a value if the endianness is different 324be3369SThomas Huth * between the target and the host. 424be3369SThomas Huth * 524be3369SThomas Huth * SPDX-License-Identifier: LGPL-2.1-or-later 624be3369SThomas Huth */ 724be3369SThomas Huth 824be3369SThomas Huth #ifndef TSWAP_H 924be3369SThomas Huth #define TSWAP_H 1024be3369SThomas Huth 1124be3369SThomas Huth #include "hw/core/cpu.h" 1224be3369SThomas Huth #include "qemu/bswap.h" 1324be3369SThomas Huth 1424be3369SThomas Huth /* 1524be3369SThomas Huth * If we're in target-specific code, we can hard-code the swapping 1624be3369SThomas Huth * condition, otherwise we have to do (slower) run-time checks. 1724be3369SThomas Huth */ 18*7d7a21baSPhilippe Mathieu-Daudé #ifdef COMPILING_PER_TARGET 1924be3369SThomas Huth #define target_needs_bswap() (HOST_BIG_ENDIAN != TARGET_BIG_ENDIAN) 2024be3369SThomas Huth #else 2124be3369SThomas Huth #define target_needs_bswap() (target_words_bigendian() != HOST_BIG_ENDIAN) 22*7d7a21baSPhilippe Mathieu-Daudé #endif /* COMPILING_PER_TARGET */ 2324be3369SThomas Huth 2424be3369SThomas Huth static inline uint16_t tswap16(uint16_t s) 2524be3369SThomas Huth { 2624be3369SThomas Huth if (target_needs_bswap()) { 2724be3369SThomas Huth return bswap16(s); 2824be3369SThomas Huth } else { 2924be3369SThomas Huth return s; 3024be3369SThomas Huth } 3124be3369SThomas Huth } 3224be3369SThomas Huth 3324be3369SThomas Huth static inline uint32_t tswap32(uint32_t s) 3424be3369SThomas Huth { 3524be3369SThomas Huth if (target_needs_bswap()) { 3624be3369SThomas Huth return bswap32(s); 3724be3369SThomas Huth } else { 3824be3369SThomas Huth return s; 3924be3369SThomas Huth } 4024be3369SThomas Huth } 4124be3369SThomas Huth 4224be3369SThomas Huth static inline uint64_t tswap64(uint64_t s) 4324be3369SThomas Huth { 4424be3369SThomas Huth if (target_needs_bswap()) { 4524be3369SThomas Huth return bswap64(s); 4624be3369SThomas Huth } else { 4724be3369SThomas Huth return s; 4824be3369SThomas Huth } 4924be3369SThomas Huth } 5024be3369SThomas Huth 5124be3369SThomas Huth static inline void tswap16s(uint16_t *s) 5224be3369SThomas Huth { 5324be3369SThomas Huth if (target_needs_bswap()) { 5424be3369SThomas Huth *s = bswap16(*s); 5524be3369SThomas Huth } 5624be3369SThomas Huth } 5724be3369SThomas Huth 5824be3369SThomas Huth static inline void tswap32s(uint32_t *s) 5924be3369SThomas Huth { 6024be3369SThomas Huth if (target_needs_bswap()) { 6124be3369SThomas Huth *s = bswap32(*s); 6224be3369SThomas Huth } 6324be3369SThomas Huth } 6424be3369SThomas Huth 6524be3369SThomas Huth static inline void tswap64s(uint64_t *s) 6624be3369SThomas Huth { 6724be3369SThomas Huth if (target_needs_bswap()) { 6824be3369SThomas Huth *s = bswap64(*s); 6924be3369SThomas Huth } 7024be3369SThomas Huth } 7124be3369SThomas Huth 7224be3369SThomas Huth #endif /* TSWAP_H */ 73