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 "qemu/bswap.h" 1224be3369SThomas Huth 1342508261SPhilippe Mathieu-Daudé /** 1442508261SPhilippe Mathieu-Daudé * target_words_bigendian: 1542508261SPhilippe Mathieu-Daudé * Returns true if the (default) endianness of the target is big endian, 16*cfac5cdfSPierrick Bouvier * false otherwise. Common code should normally never need to know about the 17*cfac5cdfSPierrick Bouvier * endianness of the target, so please do *not* use this function unless you 18*cfac5cdfSPierrick Bouvier * know very well what you are doing! 1942508261SPhilippe Mathieu-Daudé */ 2042508261SPhilippe Mathieu-Daudé bool target_words_bigendian(void); 21*cfac5cdfSPierrick Bouvier #ifdef COMPILING_PER_TARGET 22*cfac5cdfSPierrick Bouvier #define target_words_bigendian() TARGET_BIG_ENDIAN 23*cfac5cdfSPierrick Bouvier #endif 2442508261SPhilippe Mathieu-Daudé 2524be3369SThomas Huth /* 2624be3369SThomas Huth * If we're in target-specific code, we can hard-code the swapping 2724be3369SThomas Huth * condition, otherwise we have to do (slower) run-time checks. 2824be3369SThomas Huth */ 297d7a21baSPhilippe Mathieu-Daudé #ifdef COMPILING_PER_TARGET 3024be3369SThomas Huth #define target_needs_bswap() (HOST_BIG_ENDIAN != TARGET_BIG_ENDIAN) 3124be3369SThomas Huth #else 3268e05effSPhilippe Mathieu-Daudé #define target_needs_bswap() (HOST_BIG_ENDIAN != target_words_bigendian()) 337d7a21baSPhilippe Mathieu-Daudé #endif /* COMPILING_PER_TARGET */ 3424be3369SThomas Huth 3524be3369SThomas Huth static inline uint16_t tswap16(uint16_t s) 3624be3369SThomas Huth { 3724be3369SThomas Huth if (target_needs_bswap()) { 3824be3369SThomas Huth return bswap16(s); 3924be3369SThomas Huth } else { 4024be3369SThomas Huth return s; 4124be3369SThomas Huth } 4224be3369SThomas Huth } 4324be3369SThomas Huth 4424be3369SThomas Huth static inline uint32_t tswap32(uint32_t s) 4524be3369SThomas Huth { 4624be3369SThomas Huth if (target_needs_bswap()) { 4724be3369SThomas Huth return bswap32(s); 4824be3369SThomas Huth } else { 4924be3369SThomas Huth return s; 5024be3369SThomas Huth } 5124be3369SThomas Huth } 5224be3369SThomas Huth 5324be3369SThomas Huth static inline uint64_t tswap64(uint64_t s) 5424be3369SThomas Huth { 5524be3369SThomas Huth if (target_needs_bswap()) { 5624be3369SThomas Huth return bswap64(s); 5724be3369SThomas Huth } else { 5824be3369SThomas Huth return s; 5924be3369SThomas Huth } 6024be3369SThomas Huth } 6124be3369SThomas Huth 6224be3369SThomas Huth static inline void tswap16s(uint16_t *s) 6324be3369SThomas Huth { 6424be3369SThomas Huth if (target_needs_bswap()) { 6524be3369SThomas Huth *s = bswap16(*s); 6624be3369SThomas Huth } 6724be3369SThomas Huth } 6824be3369SThomas Huth 6924be3369SThomas Huth static inline void tswap32s(uint32_t *s) 7024be3369SThomas Huth { 7124be3369SThomas Huth if (target_needs_bswap()) { 7224be3369SThomas Huth *s = bswap32(*s); 7324be3369SThomas Huth } 7424be3369SThomas Huth } 7524be3369SThomas Huth 7624be3369SThomas Huth static inline void tswap64s(uint64_t *s) 7724be3369SThomas Huth { 7824be3369SThomas Huth if (target_needs_bswap()) { 7924be3369SThomas Huth *s = bswap64(*s); 8024be3369SThomas Huth } 8124be3369SThomas Huth } 8224be3369SThomas Huth 8324be3369SThomas Huth #endif /* TSWAP_H */ 84