diff --git a/fesvr/byteorder.h b/fesvr/byteorder.h index d9e503a2..71ce5155 100644 --- a/fesvr/byteorder.h +++ b/fesvr/byteorder.h @@ -3,8 +3,8 @@ #ifndef _RISCV_BYTEORDER_H #define _RISCV_BYTEORDER_H -#include "config.h" #include +#include static inline uint8_t swap(uint8_t n) { return n; } static inline uint16_t swap(uint16_t n) { return (n >> 8) | (n << 8); } @@ -22,17 +22,11 @@ static inline uint128_t swap(uint128_t n) { return (uint128_t(swap(uint64_t(n))) static inline int128_t swap(int128_t n) { return int128_t(swap(uint128_t(n))); } #endif -#ifdef WORDS_BIGENDIAN -template static inline T from_be(T n) { return n; } -template static inline T to_be(T n) { return n; } -template static inline T from_le(T n) { return swap(n); } -template static inline T to_le(T n) { return swap(n); } -#else -template static inline T from_le(T n) { return n; } -template static inline T to_le(T n) { return n; } -template static inline T from_be(T n) { return swap(n); } -template static inline T to_be(T n) { return swap(n); } -#endif +static inline bool is_be() { return htonl(1) == 1; } +template static inline T from_be(T n) { return is_be() ? n : swap(n); } +template static inline T to_be(T n) { return from_be(n); } +template static inline T from_le(T n) { return is_be() ? swap(n) : n; } +template static inline T to_le(T n) { return from_le(n); } // Wrapper to mark a value as target endian, to guide conversion code