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 #if defined (__aarch64__) 60 if (!__builtin_constant_p (x))
62 __asm__ (
"rev16 %w0, %w0":
"+r" (x));
66 return (x >> 8) | (x << 8);
78 #if defined (i386) || defined (__x86_64__) 79 if (!__builtin_constant_p (x))
81 asm volatile (
"bswap %0":
"=r" (x):
"0" (x));
84 #elif defined (__aarch64__) 85 if (!__builtin_constant_p (x))
87 __asm__ (
"rev %w0, %w0":
"+r" (x));
91 return ((x << 24) | ((x & 0xff00) << 8) | ((x >> 8) & 0xff00) | (x >> 24));
103 #if defined (__x86_64__) 104 if (!__builtin_constant_p (x))
106 asm volatile (
"bswapq %0":
"=r" (x):
"0" (x));
109 #elif defined (__aarch64__) 110 if (!__builtin_constant_p (x))
112 __asm__ (
"rev %0, %0":
"+r" (x));
117 ((((x) >> (8*(i))) & 0xff) << (8*((n)-(i)-1))) 118 return (_(x, 8, 0) | _(x, 8, 1)
119 | _(x, 8, 2) | _(x, 8, 3)
120 | _(x, 8, 4) | _(x, 8, 5) | _(x, 8, 6) | _(x, 8, 7));
130 #define _(sex,type) \ 133 clib_host_to_##sex##_##type (type x) \ 135 if (! clib_arch_is_##sex##_endian) \ 136 x = clib_byte_swap_##type (x); \ 141 clib_host_to_##sex##_mem_##type (type * x) \ 144 return clib_host_to_##sex##_##type (v); \ 148 clib_host_to_##sex##_unaligned_mem_##type (type * x) \ 150 type v = clib_mem_unaligned (x, type); \ 151 return clib_host_to_##sex##_##type (v); \ 156 clib_##sex##_to_host_##type (type x) \ 157 { return clib_host_to_##sex##_##type (x); } \ 160 clib_##sex##_to_host_mem_##type (type * x) \ 161 { return clib_host_to_##sex##_mem_##type (x); } \ 164 clib_##sex##_to_host_unaligned_mem_##type (type * x) \ 165 { return clib_host_to_##sex##_unaligned_mem_##type (x); } 180 clib_net_to_host_##type (type x) \ 181 { return clib_big_to_host_##type (x); } \ 184 clib_net_to_host_mem_##type (type * x) \ 185 { return clib_big_to_host_mem_##type (x); } \ 188 clib_net_to_host_unaligned_mem_##type (type * x) \ 189 { return clib_big_to_host_unaligned_mem_##type (x); } \ 192 clib_host_to_net_##type (type x) \ 193 { return clib_host_to_big_##type (x); } \ 196 clib_host_to_net_mem_##type (type * x) \ 197 { return clib_host_to_big_mem_##type (x); } \ 200 clib_host_to_net_unaligned_mem_##type (type * x) \ 201 { 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)