2023-04-11 21:34:17 +03:00
|
|
|
/*
|
|
|
|
* Macros for swapping a value if the endianness is different
|
|
|
|
* between the target and the host.
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: LGPL-2.1-or-later
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef TSWAP_H
|
|
|
|
#define TSWAP_H
|
|
|
|
|
|
|
|
#include "qemu/bswap.h"
|
|
|
|
|
2023-12-12 13:34:25 +03:00
|
|
|
/**
|
|
|
|
* target_words_bigendian:
|
|
|
|
* Returns true if the (default) endianness of the target is big endian,
|
|
|
|
* false otherwise. Note that in target-specific code, you can use
|
|
|
|
* TARGET_BIG_ENDIAN directly instead. On the other hand, common
|
|
|
|
* code should normally never need to know about the endianness of the
|
|
|
|
* target, so please do *not* use this function unless you know very well
|
|
|
|
* what you are doing!
|
|
|
|
*/
|
|
|
|
bool target_words_bigendian(void);
|
|
|
|
|
2023-04-11 21:34:17 +03:00
|
|
|
/*
|
|
|
|
* If we're in target-specific code, we can hard-code the swapping
|
|
|
|
* condition, otherwise we have to do (slower) run-time checks.
|
|
|
|
*/
|
2023-06-13 17:29:11 +03:00
|
|
|
#ifdef COMPILING_PER_TARGET
|
2023-04-11 21:34:17 +03:00
|
|
|
#define target_needs_bswap() (HOST_BIG_ENDIAN != TARGET_BIG_ENDIAN)
|
|
|
|
#else
|
|
|
|
#define target_needs_bswap() (target_words_bigendian() != HOST_BIG_ENDIAN)
|
2023-06-13 17:29:11 +03:00
|
|
|
#endif /* COMPILING_PER_TARGET */
|
2023-04-11 21:34:17 +03:00
|
|
|
|
|
|
|
static inline uint16_t tswap16(uint16_t s)
|
|
|
|
{
|
|
|
|
if (target_needs_bswap()) {
|
|
|
|
return bswap16(s);
|
|
|
|
} else {
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline uint32_t tswap32(uint32_t s)
|
|
|
|
{
|
|
|
|
if (target_needs_bswap()) {
|
|
|
|
return bswap32(s);
|
|
|
|
} else {
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline uint64_t tswap64(uint64_t s)
|
|
|
|
{
|
|
|
|
if (target_needs_bswap()) {
|
|
|
|
return bswap64(s);
|
|
|
|
} else {
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void tswap16s(uint16_t *s)
|
|
|
|
{
|
|
|
|
if (target_needs_bswap()) {
|
|
|
|
*s = bswap16(*s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void tswap32s(uint32_t *s)
|
|
|
|
{
|
|
|
|
if (target_needs_bswap()) {
|
|
|
|
*s = bswap32(*s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void tswap64s(uint64_t *s)
|
|
|
|
{
|
|
|
|
if (target_needs_bswap()) {
|
|
|
|
*s = bswap64(*s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* TSWAP_H */
|