| 1 | /* |
| 2 | * Macros for swapping a value if the endianness is different |
| 3 | * between the target and the host. |
| 4 | * |
| 5 | * SPDX-License-Identifier: LGPL-2.1-or-later |
| 6 | */ |
| 7 | |
| 8 | #ifndef TSWAP_H |
| 9 | #define TSWAP_H |
| 10 | |
| 11 | #include "qemu/bswap.h" |
| 12 | #include "qemu/target-info.h" |
| 13 | #include "hw/core/cpu.h" |
| 14 | |
| 15 | /* |
| 16 | * If we're in target-specific code, we can hard-code the swapping |
| 17 | * condition, otherwise we have to do (slower) run-time checks. |
| 18 | */ |
| 19 | #ifdef COMPILING_PER_TARGET |
| 20 | #define target_needs_bswap() (HOST_BIG_ENDIAN != TARGET_BIG_ENDIAN) |
| 21 | #else |
| 22 | #define target_needs_bswap() (HOST_BIG_ENDIAN != target_big_endian()) |
| 23 | #endif /* COMPILING_PER_TARGET */ |
| 24 | |
| 25 | #if defined(CONFIG_USER_ONLY) \ |
| 26 | || !defined(TARGET_NOT_USING_LEGACY_NATIVE_ENDIAN_API) |
| 27 | static inline uint16_t tswap16(uint16_t s) |
| 28 | { |
| 29 | if (target_needs_bswap()) { |
| 30 | return bswap16(s); |
| 31 | } else { |
| 32 | return s; |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | static inline uint32_t tswap32(uint32_t s) |
| 37 | { |
| 38 | if (target_needs_bswap()) { |
| 39 | return bswap32(s); |
| 40 | } else { |
| 41 | return s; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | static inline uint64_t tswap64(uint64_t s) |
| 46 | { |
| 47 | if (target_needs_bswap()) { |
| 48 | return bswap64(s); |
| 49 | } else { |
| 50 | return s; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | static inline void tswap16s(uint16_t *s) |
| 55 | { |
| 56 | if (target_needs_bswap()) { |
| 57 | *s = bswap16(*s); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | static inline void tswap32s(uint32_t *s) |
| 62 | { |
| 63 | if (target_needs_bswap()) { |
| 64 | *s = bswap32(*s); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | static inline void tswap64s(uint64_t *s) |
| 69 | { |
| 70 | if (target_needs_bswap()) { |
| 71 | *s = bswap64(*s); |
| 72 | } |
| 73 | } |
| 74 | #endif |
| 75 | |
| 76 | /* |
| 77 | * If we're in semihosting code, have to swap depending on the currently |
| 78 | * configured endianness of the CPU. These functions should not be used in |
| 79 | * other contexts. |
| 80 | */ |
| 81 | #define cpu_internal_needs_bswap(cpu) \ |
| 82 | (HOST_BIG_ENDIAN != cpu_internal_is_big_endian(cpu)) |
| 83 | |
| 84 | static inline uint16_t cpu_internal_tswap16(CPUState *cpu, uint16_t s) |
| 85 | { |
| 86 | if (cpu_internal_needs_bswap(cpu)) { |
| 87 | return bswap16(s); |
| 88 | } else { |
| 89 | return s; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | static inline uint32_t cpu_internal_tswap32(CPUState *cpu, uint32_t s) |
| 94 | { |
| 95 | if (cpu_internal_needs_bswap(cpu)) { |
| 96 | return bswap32(s); |
| 97 | } else { |
| 98 | return s; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | static inline uint64_t cpu_internal_tswap64(CPUState *cpu, uint64_t s) |
| 103 | { |
| 104 | if (cpu_internal_needs_bswap(cpu)) { |
| 105 | return bswap64(s); |
| 106 | } else { |
| 107 | return s; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | #endif /* TSWAP_H */ |