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");
149 #if (defined(_BIG_ENDIAN) || !defined(_LITTLE_ENDIAN)) && BYTE_ORDER == BIG_ENDIAN
153 template <
typename T>
inline T
htobe(T value) {
return value;}
154 template <
typename T>
inline T
betoh(T value) {
return value;}
155 #elif defined(_LITTLE_ENDIAN) || BYTE_ORDER == LITTLE_ENDIAN
157 template <
typename T>
inline T
htole(T value) {
return value;}
158 template <
typename T>
inline T
letoh(T value) {
return value;}
159 template <
typename T>
inline T
htobe(T value) {
return swap_byte(value);}
160 template <
typename T>
inline T
betoh(T value) {
return swap_byte(value);}
162 #error Invalid Endianess
165 namespace BigEndianGuest
168 template <
typename T>
170 template <
typename T>
172 template <
typename T>
173 inline T
gtobe(T value) {
return value;}
174 template <
typename T>
175 inline T
betog(T value) {
return value;}
176 template <
typename T>
178 template <
typename T>
182 namespace LittleEndianGuest
185 template <
typename T>
186 inline T
gtole(T value) {
return value;}
187 template <
typename T>
188 inline T
letog(T value) {
return value;}
189 template <
typename T>
191 template <
typename T>
193 template <
typename T>
195 template <
typename T>
198 #endif // __SIM_BYTE_SWAP_HH__
Twin64_t swap_byte< Twin64_t >(Twin64_t x)
const ByteOrder GuestByteOrder
const ByteOrder HostByteOrder
const ByteOrder GuestByteOrder
Twin32_t swap_byte< Twin32_t >(Twin32_t x)
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)