FD.io VPP  v17.10-9-gd594711
Vector Packet Processing
byte_order.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 /*
16  Copyright (c) 2004 Eliot Dresselhaus
17 
18  Permission is hereby granted, free of charge, to any person obtaining
19  a copy of this software and associated documentation files (the
20  "Software"), to deal in the Software without restriction, including
21  without limitation the rights to use, copy, modify, merge, publish,
22  distribute, sublicense, and/or sell copies of the Software, and to
23  permit persons to whom the Software is furnished to do so, subject to
24  the following conditions:
25 
26  The above copyright notice and this permission notice shall be
27  included in all copies or substantial portions of the Software.
28 
29  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
30  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
32  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
33  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
34  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
35  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 */
37 
38 #ifndef included_clib_byte_order_h
39 #define included_clib_byte_order_h
40 
41 #include <vppinfra/clib.h>
42 
43 #if (__BYTE_ORDER__)==( __ORDER_LITTLE_ENDIAN__)
44 #define CLIB_ARCH_IS_BIG_ENDIAN (0)
45 #define CLIB_ARCH_IS_LITTLE_ENDIAN (1)
46 #else
47 /* Default is big endian. */
48 #define CLIB_ARCH_IS_BIG_ENDIAN (1)
49 #define CLIB_ARCH_IS_LITTLE_ENDIAN (0)
50 #endif
51 
52 /* Big/little endian. */
53 #define clib_arch_is_big_endian CLIB_ARCH_IS_BIG_ENDIAN
54 #define clib_arch_is_little_endian CLIB_ARCH_IS_LITTLE_ENDIAN
55 
58 {
59  return (x >> 8) | (x << 8);
60 }
61 
64 {
65  return clib_byte_swap_u16 (x);
66 }
67 
70 {
71 #if defined (i386) || defined (__x86_64__)
72  if (!__builtin_constant_p (x))
73  {
74  asm volatile ("bswap %0":"=r" (x):"0" (x));
75  return x;
76  }
77 #endif
78  return ((x << 24) | ((x & 0xff00) << 8) | ((x >> 8) & 0xff00) | (x >> 24));
79 }
80 
83 {
84  return clib_byte_swap_u32 (x);
85 }
86 
89 {
90 #if defined (__x86_64__)
91  if (!__builtin_constant_p (x))
92  {
93  asm volatile ("bswapq %0":"=r" (x):"0" (x));
94  return x;
95  }
96 #endif
97 #define _(x,n,i) \
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));
102 #undef _
103 }
104 
107 {
108  return clib_byte_swap_u64 (x);
109 }
110 
111 #define _(sex,type) \
112 /* HOST -> SEX */ \
113 always_inline type \
114 clib_host_to_##sex##_##type (type x) \
115 { \
116  if (! clib_arch_is_##sex##_endian) \
117  x = clib_byte_swap_##type (x); \
118  return x; \
119 } \
120  \
121 always_inline type \
122 clib_host_to_##sex##_mem_##type (type * x) \
123 { \
124  type v = x[0]; \
125  return clib_host_to_##sex##_##type (v); \
126 } \
127  \
128 always_inline type \
129 clib_host_to_##sex##_unaligned_mem_##type (type * x) \
130 { \
131  type v = clib_mem_unaligned (x, type); \
132  return clib_host_to_##sex##_##type (v); \
133 } \
134  \
135 /* SEX -> HOST */ \
136 always_inline type \
137 clib_##sex##_to_host_##type (type x) \
138 { return clib_host_to_##sex##_##type (x); } \
139  \
140 always_inline type \
141 clib_##sex##_to_host_mem_##type (type * x) \
142 { return clib_host_to_##sex##_mem_##type (x); } \
143  \
144 always_inline type \
145 clib_##sex##_to_host_unaligned_mem_##type (type * x) \
146 { return clib_host_to_##sex##_unaligned_mem_##type (x); }
147 
148 #ifndef __cplusplus
149 _(little, u16)
150 _(little, u32)
151 _(little, u64)
152 _(little, i16)
153 _(little, i32)
154 _(little, i64)
155 _(big, u16) _(big, u32) _(big, u64) _(big, i16) _(big, i32) _(big, i64)
156 #endif
157 #undef _
158 /* Network "net" alias for "big". */
159 #define _(type) \
160 always_inline type \
161 clib_net_to_host_##type (type x) \
162 { return clib_big_to_host_##type (x); } \
163  \
164 always_inline type \
165 clib_net_to_host_mem_##type (type * x) \
166 { return clib_big_to_host_mem_##type (x); } \
167  \
168 always_inline type \
169 clib_net_to_host_unaligned_mem_##type (type * x) \
170 { return clib_big_to_host_unaligned_mem_##type (x); } \
171  \
172 always_inline type \
173 clib_host_to_net_##type (type x) \
174 { return clib_host_to_big_##type (x); } \
175  \
176 always_inline type \
177 clib_host_to_net_mem_##type (type * x) \
178 { return clib_host_to_big_mem_##type (x); } \
179  \
180 always_inline type \
181 clib_host_to_net_unaligned_mem_##type (type * x) \
182 { return clib_host_to_big_unaligned_mem_##type (x); }
183 #ifndef __cplusplus
184  _(u16);
185 _(i16);
186 _(u32);
187 _(i32);
188 _(u64);
189 _(i64);
190 #endif
191 
192 #undef _
193 
194 #endif /* included_clib_byte_order_h */
195 
196 /*
197  * fd.io coding-style-patch-verification: ON
198  *
199  * Local Variables:
200  * eval: (c-set-style "gnu")
201  * End:
202  */
static u32 clib_byte_swap_u32(u32 x)
Definition: byte_order.h:69
static i64 clib_byte_swap_i64(i64 x)
Definition: byte_order.h:106
#define always_inline
Definition: clib.h:84
static i32 clib_byte_swap_i32(i32 x)
Definition: byte_order.h:82
int i32
Definition: types.h:81
static i16 clib_byte_swap_i16(i16 x)
Definition: byte_order.h:63
unsigned long u64
Definition: types.h:89
static u64 clib_byte_swap_u64(u64 x)
Definition: byte_order.h:88
long i64
Definition: types.h:82
unsigned int u32
Definition: types.h:88
unsigned short u16
Definition: types.h:57
short i16
Definition: types.h:46
static u16 clib_byte_swap_u16(u16 x)
Definition: byte_order.h:57