38 #ifndef included_clib_byte_order_h 39 #define included_clib_byte_order_h 43 #if (__BYTE_ORDER__)==( __ORDER_LITTLE_ENDIAN__) 44 #define CLIB_ARCH_IS_BIG_ENDIAN (0) 45 #define CLIB_ARCH_IS_LITTLE_ENDIAN (1) 48 #define CLIB_ARCH_IS_BIG_ENDIAN (1) 49 #define CLIB_ARCH_IS_LITTLE_ENDIAN (0) 53 #define clib_arch_is_big_endian CLIB_ARCH_IS_BIG_ENDIAN 54 #define clib_arch_is_little_endian CLIB_ARCH_IS_LITTLE_ENDIAN 59 return (x >> 8) | (x << 8);
71 #if defined (i386) || defined (__x86_64__) 72 if (!__builtin_constant_p (x))
74 asm volatile (
"bswap %0":
"=r" (x):
"0" (x));
78 return ((x << 24) | ((x & 0xff00) << 8) | ((x >> 8) & 0xff00) | (x >> 24));
90 #if defined (__x86_64__) 91 if (!__builtin_constant_p (x))
93 asm volatile (
"bswapq %0":
"=r" (x):
"0" (x));
98 ((((x) >> (8*(i))) & 0xff) << (8*((n)-(i)-1))) 99 return (_(x, 8, 0) | _(x, 8, 1)
100 | _(x, 8, 2) | _(x, 8, 3)
101 | _(x, 8, 4) | _(x, 8, 5) | _(x, 8, 6) | _(x, 8, 7));
111 #define _(sex,type) \ 114 clib_host_to_##sex##_##type (type x) \ 116 if (! clib_arch_is_##sex##_endian) \ 117 x = clib_byte_swap_##type (x); \ 122 clib_host_to_##sex##_mem_##type (type * x) \ 125 return clib_host_to_##sex##_##type (v); \ 129 clib_host_to_##sex##_unaligned_mem_##type (type * x) \ 131 type v = clib_mem_unaligned (x, type); \ 132 return clib_host_to_##sex##_##type (v); \ 137 clib_##sex##_to_host_##type (type x) \ 138 { return clib_host_to_##sex##_##type (x); } \ 141 clib_##sex##_to_host_mem_##type (type * x) \ 142 { return clib_host_to_##sex##_mem_##type (x); } \ 145 clib_##sex##_to_host_unaligned_mem_##type (type * x) \ 146 { return clib_host_to_##sex##_unaligned_mem_##type (x); } 161 clib_net_to_host_##type (type x) \ 162 { return clib_big_to_host_##type (x); } \ 165 clib_net_to_host_mem_##type (type * x) \ 166 { return clib_big_to_host_mem_##type (x); } \ 169 clib_net_to_host_unaligned_mem_##type (type * x) \ 170 { return clib_big_to_host_unaligned_mem_##type (x); } \ 173 clib_host_to_net_##type (type x) \ 174 { return clib_host_to_big_##type (x); } \ 177 clib_host_to_net_mem_##type (type * x) \ 178 { return clib_host_to_big_mem_##type (x); } \ 181 clib_host_to_net_unaligned_mem_##type (type * x) \ 182 { return clib_host_to_big_unaligned_mem_##type (x); } static u32 clib_byte_swap_u32(u32 x)
static i64 clib_byte_swap_i64(i64 x)
static i32 clib_byte_swap_i32(i32 x)
static i16 clib_byte_swap_i16(i16 x)
static u64 clib_byte_swap_u64(u64 x)
static u16 clib_byte_swap_u16(u16 x)