From 97a7244371627f37f8086259002e4ed2894f5d0e Mon Sep 17 00:00:00 2001 From: Andrew Waterman Date: Tue, 16 Jul 2024 19:43:59 -0700 Subject: [PATCH] Avoid dependence on config.h in byteorder.h The runtime check will compile out. --- fesvr/byteorder.h | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) 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