33 #ifndef __SIM_BYTE_SWAP_HH__
34 #define __SIM_BYTE_SWAP_HH__
40 #if defined(__linux__)
47 #include <sys/isa_defs.h>
49 #include <machine/endian.h>
52 #if defined(__APPLE__)
53 #include <libkern/OSByteOrder.h>
61 #if defined(__linux__)
63 #elif defined(__APPLE__)
64 return OSSwapInt64(
x);
66 return (uint64_t)((((uint64_t)(
x) & 0xff) << 56) |
67 ((uint64_t)(
x) & 0xff00
ULL) << 40 |
68 ((uint64_t)(
x) & 0xff0000ULL) << 24 |
69 ((uint64_t)(
x) & 0xff000000
ULL) << 8 |
70 ((uint64_t)(
x) & 0xff00000000ULL) >> 8 |
71 ((uint64_t)(
x) & 0xff0000000000
ULL) >> 24 |
72 ((uint64_t)(
x) & 0xff000000000000ULL) >> 40 |
73 ((uint64_t)(
x) & 0xff00000000000000
ULL) >> 56) ;
80 #if defined(__linux__)
82 #elif defined(__APPLE__)
83 return OSSwapInt32(
x);
85 return (uint32_t)(((uint32_t)(
x) & 0xff) << 24 |
86 ((uint32_t)(
x) & 0xff00) << 8 | ((uint32_t)(
x) & 0xff0000) >> 8 |
87 ((uint32_t)(
x) & 0xff000000) >> 24);
94 #if defined(__linux__)
96 #elif defined(__APPLE__)
97 return OSSwapInt16(
x);
99 return (uint16_t)(((uint16_t)(
x) & 0xff) << 8 |
100 ((uint16_t)(
x) & 0xff00) >> 8);
108 template <
typename T>
112 else if (
sizeof(T) == 4)
114 else if (
sizeof(T) == 2)
116 else if (
sizeof(T) == 1)
119 panic(
"Can't byte-swap values larger than 64 bits");
122 template <
typename T,
size_t N>
123 inline std::array<T, N>
138 #if (defined(_BIG_ENDIAN) || !defined(_LITTLE_ENDIAN)) && BYTE_ORDER == BIG_ENDIAN
142 template <
typename T>
inline T
htobe(T value) {
return value;}
143 template <
typename T>
inline T
betoh(T value) {
return value;}
144 #elif defined(_LITTLE_ENDIAN) || BYTE_ORDER == LITTLE_ENDIAN
146 template <
typename T>
inline T
htole(T value) {
return value;}
147 template <
typename T>
inline T
letoh(T value) {
return value;}
148 template <
typename T>
inline T
htobe(T value) {
return swap_byte(value);}
149 template <
typename T>
inline T
betoh(T value) {
return swap_byte(value);}
151 #error Invalid Endianess
154 template <
typename T>
155 inline T
htog(T value, ByteOrder guest_byte_order)
157 return guest_byte_order == ByteOrder::big ?
161 template <
typename T>
162 inline T
gtoh(T value, ByteOrder guest_byte_order)
164 return guest_byte_order == ByteOrder::big ?
168 #endif // __SIM_BYTE_SWAP_HH__