xref: /openbmc/qemu/include/exec/tswap.h (revision 08ae519ab8eb6c9abbd97156cb3678f372521501)
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,
1642508261SPhilippe Mathieu-Daudé  * false otherwise. Note that in target-specific code, you can use
1742508261SPhilippe Mathieu-Daudé  * TARGET_BIG_ENDIAN directly instead. On the other hand, common
1842508261SPhilippe Mathieu-Daudé  * code should normally never need to know about the endianness of the
1942508261SPhilippe Mathieu-Daudé  * target, so please do *not* use this function unless you know very well
2042508261SPhilippe Mathieu-Daudé  * what you are doing!
2142508261SPhilippe Mathieu-Daudé  */
2242508261SPhilippe Mathieu-Daudé bool target_words_bigendian(void);
2342508261SPhilippe Mathieu-Daudé 
2424be3369SThomas Huth /*
2524be3369SThomas Huth  * If we're in target-specific code, we can hard-code the swapping
2624be3369SThomas Huth  * condition, otherwise we have to do (slower) run-time checks.
2724be3369SThomas Huth  */
287d7a21baSPhilippe Mathieu-Daudé #ifdef COMPILING_PER_TARGET
2924be3369SThomas Huth #define target_needs_bswap()  (HOST_BIG_ENDIAN != TARGET_BIG_ENDIAN)
3024be3369SThomas Huth #else
31*68e05effSPhilippe Mathieu-Daudé #define target_needs_bswap()  (HOST_BIG_ENDIAN != target_words_bigendian())
327d7a21baSPhilippe Mathieu-Daudé #endif /* COMPILING_PER_TARGET */
3324be3369SThomas Huth 
tswap16(uint16_t s)3424be3369SThomas Huth static inline uint16_t tswap16(uint16_t s)
3524be3369SThomas Huth {
3624be3369SThomas Huth     if (target_needs_bswap()) {
3724be3369SThomas Huth         return bswap16(s);
3824be3369SThomas Huth     } else {
3924be3369SThomas Huth         return s;
4024be3369SThomas Huth     }
4124be3369SThomas Huth }
4224be3369SThomas Huth 
tswap32(uint32_t s)4324be3369SThomas Huth static inline uint32_t tswap32(uint32_t s)
4424be3369SThomas Huth {
4524be3369SThomas Huth     if (target_needs_bswap()) {
4624be3369SThomas Huth         return bswap32(s);
4724be3369SThomas Huth     } else {
4824be3369SThomas Huth         return s;
4924be3369SThomas Huth     }
5024be3369SThomas Huth }
5124be3369SThomas Huth 
tswap64(uint64_t s)5224be3369SThomas Huth static inline uint64_t tswap64(uint64_t s)
5324be3369SThomas Huth {
5424be3369SThomas Huth     if (target_needs_bswap()) {
5524be3369SThomas Huth         return bswap64(s);
5624be3369SThomas Huth     } else {
5724be3369SThomas Huth         return s;
5824be3369SThomas Huth     }
5924be3369SThomas Huth }
6024be3369SThomas Huth 
tswap16s(uint16_t * s)6124be3369SThomas Huth static inline void tswap16s(uint16_t *s)
6224be3369SThomas Huth {
6324be3369SThomas Huth     if (target_needs_bswap()) {
6424be3369SThomas Huth         *s = bswap16(*s);
6524be3369SThomas Huth     }
6624be3369SThomas Huth }
6724be3369SThomas Huth 
tswap32s(uint32_t * s)6824be3369SThomas Huth static inline void tswap32s(uint32_t *s)
6924be3369SThomas Huth {
7024be3369SThomas Huth     if (target_needs_bswap()) {
7124be3369SThomas Huth         *s = bswap32(*s);
7224be3369SThomas Huth     }
7324be3369SThomas Huth }
7424be3369SThomas Huth 
tswap64s(uint64_t * s)7524be3369SThomas Huth static inline void tswap64s(uint64_t *s)
7624be3369SThomas Huth {
7724be3369SThomas Huth     if (target_needs_bswap()) {
7824be3369SThomas Huth         *s = bswap64(*s);
7924be3369SThomas Huth     }
8024be3369SThomas Huth }
8124be3369SThomas Huth 
8224be3369SThomas Huth #endif  /* TSWAP_H */
83