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 58 {
return (x >> 8) | (x << 8); }
67 #if defined (i386) || defined (__x86_64__) 68 if (! __builtin_constant_p (x))
70 asm volatile (
"bswap %0" :
"=r" (x) :
"0" (x));
87 #if defined (__x86_64__) 88 if (! __builtin_constant_p (x))
90 asm volatile (
"bswapq %0" :
"=r" (x) :
"0" (x));
95 ((((x) >> (8*(i))) & 0xff) << (8*((n)-(i)-1))) 96 return (_ (x, 8, 0) | _ (x, 8, 1)
97 | _ (x, 8, 2) | _ (x, 8, 3)
98 | _ (x, 8, 4) | _ (x, 8, 5)
99 | _ (x, 8, 6) | _ (x, 8, 7));
107 #define _(sex,type) \ 110 clib_host_to_##sex##_##type (type x) \ 112 if (! clib_arch_is_##sex##_endian) \ 113 x = clib_byte_swap_##type (x); \ 118 clib_host_to_##sex##_mem_##type (type * x) \ 121 return clib_host_to_##sex##_##type (v); \ 125 clib_host_to_##sex##_unaligned_mem_##type (type * x) \ 127 type v = clib_mem_unaligned (x, type); \ 128 return clib_host_to_##sex##_##type (v); \ 133 clib_##sex##_to_host_##type (type x) \ 134 { return clib_host_to_##sex##_##type (x); } \ 137 clib_##sex##_to_host_mem_##type (type * x) \ 138 { return clib_host_to_##sex##_mem_##type (x); } \ 141 clib_##sex##_to_host_unaligned_mem_##type (type * x) \ 142 { return clib_host_to_##sex##_unaligned_mem_##type (x); } 164 clib_net_to_host_##type (type x) \ 165 { return clib_big_to_host_##type (x); } \ 168 clib_net_to_host_mem_##type (type * x) \ 169 { return clib_big_to_host_mem_##type (x); } \ 172 clib_net_to_host_unaligned_mem_##type (type * x) \ 173 { return clib_big_to_host_unaligned_mem_##type (x); } \ 176 clib_host_to_net_##type (type x) \ 177 { return clib_host_to_big_##type (x); } \ 180 clib_host_to_net_mem_##type (type * x) \ 181 { return clib_host_to_big_mem_##type (x); } \ 184 clib_host_to_net_unaligned_mem_##type (type * x) \ 185 { return clib_host_to_big_unaligned_mem_##type (x); } always_inline i32 clib_byte_swap_i32(i32 x)
always_inline i16 clib_byte_swap_i16(i16 x)
always_inline u32 clib_byte_swap_u32(u32 x)
always_inline i64 clib_byte_swap_i64(i64 x)
always_inline u64 clib_byte_swap_u64(u64 x)
always_inline u16 clib_byte_swap_u16(u16 x)