30 #ifndef TINS_ENDIANNESS_H 31 #define TINS_ENDIANNESS_H 34 #include <tins/macros.h> 36 #if defined(__APPLE__) 37 #include <sys/types.h> 38 #define TINS_IS_LITTLE_ENDIAN (BYTE_ORDER == LITTLE_ENDIAN) 39 #define TINS_IS_BIG_ENDIAN (BYTE_ORDER == BIG_ENDIAN) 41 #include <sys/endian.h> 42 #define TINS_IS_LITTLE_ENDIAN (_BYTE_ORDER == _LITTLE_ENDIAN) 43 #define TINS_IS_BIG_ENDIAN (_BYTE_ORDER == _BIG_ENDIAN) 47 #define TINS_IS_LITTLE_ENDIAN 1 48 #define TINS_IS_BIG_ENDIAN 0 51 #define TINS_IS_LITTLE_ENDIAN (__BYTE_ORDER == __LITTLE_ENDIAN) 52 #define TINS_IS_BIG_ENDIAN (__BYTE_ORDER == __BIG_ENDIAN) 57 #define TINS_BYTE_SWAP_16(data) _byteswap_ushort(data) 58 #define TINS_BYTE_SWAP_32(data) _byteswap_ulong(data) 59 #define TINS_BYTE_SWAP_64(data) _byteswap_uint64(data) 60 #elif defined(TINS_HAVE_GCC_BUILTIN_SWAP) 61 #define TINS_BYTE_SWAP_16(data) __builtin_bswap16(data) 62 #define TINS_BYTE_SWAP_32(data) __builtin_bswap32(data) 63 #define TINS_BYTE_SWAP_64(data) __builtin_bswap64(data) 65 #define TINS_NO_BYTE_SWAP_INTRINSICS 77 inline uint8_t do_change_endian(uint8_t data) {
86 inline uint16_t do_change_endian(uint16_t data) {
87 #ifdef TINS_NO_BYTE_SWAP_INTRINSICS 88 return ((data & 0xff00) >> 8) | ((data & 0x00ff) << 8);
90 return TINS_BYTE_SWAP_16(data);
99 inline uint32_t do_change_endian(uint32_t data) {
100 #ifdef TINS_NO_BYTE_SWAP_INTRINSICS 101 return (((data & 0xff000000) >> 24) | ((data & 0x00ff0000) >> 8) |
102 ((data & 0x0000ff00) << 8) | ((data & 0x000000ff) << 24));
104 return TINS_BYTE_SWAP_32(data);
113 inline uint64_t do_change_endian(uint64_t data) {
114 #ifdef TINS_NO_BYTE_SWAP_INTRINSICS 115 return (((uint64_t)(do_change_endian((uint32_t)(data & 0xffffffff))) << 32) |
116 (do_change_endian(((uint32_t)(data >> 32)))));
118 return TINS_BYTE_SWAP_64(data);
128 struct conversion_dispatch_helper {
129 static T dispatch(T data) {
130 return do_change_endian(data);
136 struct conversion_dispatcher;
139 struct conversion_dispatcher<sizeof(uint8_t)>
140 :
public conversion_dispatch_helper<uint8_t> { };
143 struct conversion_dispatcher<sizeof(uint16_t)>
144 :
public conversion_dispatch_helper<uint16_t> { };
147 struct conversion_dispatcher<sizeof(uint32_t)>
148 :
public conversion_dispatch_helper<uint32_t> { };
151 struct conversion_dispatcher<sizeof(uint64_t)>
152 :
public conversion_dispatch_helper<uint64_t> { };
166 inline T change_endian(T data) {
167 return conversion_dispatcher<sizeof(T)>::dispatch(data);
170 #if TINS_IS_LITTLE_ENDIAN 177 inline T host_to_be(T data) {
178 return change_endian(data);
189 inline T host_to_le(T data) {
199 inline T be_to_host(T data) {
200 return change_endian(data);
209 inline T le_to_host(T data) {
212 #elif TINS_IS_BIG_ENDIAN 219 inline T host_to_be(T data) {
231 inline T host_to_le(T data) {
232 return change_endian(data);
241 inline T be_to_host(T data) {
251 inline T le_to_host(T data) {
252 return change_endian(data);
259 #endif // TINS_ENDIANNESS_H The Tins namespace.
Definition: address_range.h:38