37 #ifndef __SIM_BYTE_SWAP_HH__ 38 #define __SIM_BYTE_SWAP_HH__ 44 #if defined(__linux__) 51 #include <sys/isa_defs.h> 53 #include <machine/endian.h> 56 #if defined(__APPLE__) 57 #include <libkern/OSByteOrder.h> 65 #if defined(__linux__) 67 #elif defined(__APPLE__) 68 return OSSwapInt64(x);
70 return (uint64_t)((((uint64_t)(x) & 0xff) << 56) |
71 ((uint64_t)(x) & 0xff00
ULL) << 40 |
72 ((uint64_t)(
x) & 0xff0000ULL) << 24 |
73 ((uint64_t)(x) & 0xff000000
ULL) << 8 |
74 ((uint64_t)(
x) & 0xff00000000ULL) >> 8 |
75 ((uint64_t)(x) & 0xff0000000000
ULL) >> 24 |
76 ((uint64_t)(
x) & 0xff000000000000ULL) >> 40 |
77 ((uint64_t)(x) & 0xff00000000000000
ULL) >> 56) ;
84 #if defined(__linux__) 86 #elif defined(__APPLE__) 87 return OSSwapInt32(x);
89 return (uint32_t)(((uint32_t)(x) & 0xff) << 24 |
90 ((uint32_t)(
x) & 0xff00) << 8 | ((uint32_t)(x) & 0xff0000) >> 8 |
91 ((uint32_t)(
x) & 0xff000000) >> 24);
98 #if defined(__linux__) 100 #elif defined(__APPLE__) 101 return OSSwapInt16(x);
103 return (uint16_t)(((uint16_t)(x) & 0xff) << 8 |
104 ((uint16_t)(
x) & 0xff00) >> 8);
112 template <
typename T>
116 else if (
sizeof(T) == 4)
118 else if (
sizeof(T) == 2)
120 else if (
sizeof(T) == 1)
123 panic(
"Can't byte-swap values larger than 64 bits");
126 template <
typename T,
size_t N>
127 inline std::array<T, N>
142 #if (defined(_BIG_ENDIAN) || !defined(_LITTLE_ENDIAN)) && BYTE_ORDER == BIG_ENDIAN 146 template <
typename T>
inline T
htobe(T value) {
return value;}
147 template <
typename T>
inline T
betoh(T value) {
return value;}
148 #elif defined(_LITTLE_ENDIAN) || BYTE_ORDER == LITTLE_ENDIAN 150 template <
typename T>
inline T
htole(T value) {
return value;}
151 template <
typename T>
inline T
letoh(T value) {
return value;}
152 template <
typename T>
inline T
htobe(T value) {
return swap_byte(value);}
153 template <
typename T>
inline T
betoh(T value) {
return swap_byte(value);}
155 #error Invalid Endianess 158 template <
typename T>
165 template <
typename T>
172 #endif // __SIM_BYTE_SWAP_HH__ #define panic(...)
This implements a cprintf based panic() function.
T gtoh(T value, ByteOrder guest_byte_order)
const ByteOrder HostByteOrder
T htog(T value, ByteOrder guest_byte_order)
Defines global host-dependent types: Counter, Tick, and (indirectly) {int,uint}{8,16,32,64}_t.
#define ULL(N)
uint64_t constant
uint32_t swap_byte32(uint32_t x)
uint64_t swap_byte64(uint64_t x)
uint16_t swap_byte16(uint16_t x)