FD.io VPP  v19.04-6-g6f05f72
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 #if defined (__aarch64__)
60  if (!__builtin_constant_p (x))
61  {
62  __asm__ ("rev16 %w0, %w0":"+r" (x));
63  return x;
64  }
65 #endif
66  return (x >> 8) | (x << 8);
67 }
68 
71 {
72  return clib_byte_swap_u16 (x);
73 }
74 
77 {
78 #if defined (i386) || defined (__x86_64__)
79  if (!__builtin_constant_p (x))
80  {
81  asm volatile ("bswap %0":"=r" (x):"0" (x));
82  return x;
83  }
84 #elif defined (__aarch64__)
85  if (!__builtin_constant_p (x))
86  {
87  __asm__ ("rev %w0, %w0":"+r" (x));
88  return x;
89  }
90 #endif
91  return ((x << 24) | ((x & 0xff00) << 8) | ((x >> 8) & 0xff00) | (x >> 24));
92 }
93 
96 {
97  return clib_byte_swap_u32 (x);
98 }
99 
102 {
103 #if defined (__x86_64__)
104  if (!__builtin_constant_p (x))
105  {
106  asm volatile ("bswapq %0":"=r" (x):"0" (x));
107  return x;
108  }
109 #elif defined (__aarch64__)
110  if (!__builtin_constant_p (x))
111  {
112  __asm__ ("rev %0, %0":"+r" (x));
113  return x;
114  }
115 #endif
116 #define _(x,n,i) \
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));
121 #undef _
122 }
123 
126 {
127  return clib_byte_swap_u64 (x);
128 }
129 
130 #define _(sex,type) \
131 /* HOST -> SEX */ \
132 always_inline type \
133 clib_host_to_##sex##_##type (type x) \
134 { \
135  if (! clib_arch_is_##sex##_endian) \
136  x = clib_byte_swap_##type (x); \
137  return x; \
138 } \
139  \
140 always_inline type \
141 clib_host_to_##sex##_mem_##type (type * x) \
142 { \
143  type v = x[0]; \
144  return clib_host_to_##sex##_##type (v); \
145 } \
146  \
147 always_inline type \
148 clib_host_to_##sex##_unaligned_mem_##type (type * x) \
149 { \
150  type v = clib_mem_unaligned (x, type); \
151  return clib_host_to_##sex##_##type (v); \
152 } \
153  \
154 /* SEX -> HOST */ \
155 always_inline type \
156 clib_##sex##_to_host_##type (type x) \
157 { return clib_host_to_##sex##_##type (x); } \
158  \
159 always_inline type \
160 clib_##sex##_to_host_mem_##type (type * x) \
161 { return clib_host_to_##sex##_mem_##type (x); } \
162  \
163 always_inline type \
164 clib_##sex##_to_host_unaligned_mem_##type (type * x) \
165 { return clib_host_to_##sex##_unaligned_mem_##type (x); }
166 
167 #ifndef __cplusplus
168 _(little, u16)
169 _(little, u32)
170 _(little, u64)
171 _(little, i16)
172 _(little, i32)
173 _(little, i64)
174 _(big, u16) _(big, u32) _(big, u64) _(big, i16) _(big, i32) _(big, i64)
175 #endif
176 #undef _
177 /* Network "net" alias for "big". */
178 #define _(type) \
179 always_inline type \
180 clib_net_to_host_##type (type x) \
181 { return clib_big_to_host_##type (x); } \
182  \
183 always_inline type \
184 clib_net_to_host_mem_##type (type * x) \
185 { return clib_big_to_host_mem_##type (x); } \
186  \
187 always_inline type \
188 clib_net_to_host_unaligned_mem_##type (type * x) \
189 { return clib_big_to_host_unaligned_mem_##type (x); } \
190  \
191 always_inline type \
192 clib_host_to_net_##type (type x) \
193 { return clib_host_to_big_##type (x); } \
194  \
195 always_inline type \
196 clib_host_to_net_mem_##type (type * x) \
197 { return clib_host_to_big_mem_##type (x); } \
198  \
199 always_inline type \
200 clib_host_to_net_unaligned_mem_##type (type * x) \
201 { return clib_host_to_big_unaligned_mem_##type (x); }
202 #ifndef __cplusplus
203  _(u16);
204 _(i16);
205 _(u32);
206 _(i32);
207 _(u64);
208 _(i64);
209 #endif
210 
211 #undef _
212 
213 #endif /* included_clib_byte_order_h */
214 
215 /*
216  * fd.io coding-style-patch-verification: ON
217  *
218  * Local Variables:
219  * eval: (c-set-style "gnu")
220  * End:
221  */
unsigned long u64
Definition: types.h:89
static u32 clib_byte_swap_u32(u32 x)
Definition: byte_order.h:76
static i64 clib_byte_swap_i64(i64 x)
Definition: byte_order.h:125
#define always_inline
Definition: clib.h:98
static i32 clib_byte_swap_i32(i32 x)
Definition: byte_order.h:95
static i16 clib_byte_swap_i16(i16 x)
Definition: byte_order.h:70
static u64 clib_byte_swap_u64(u64 x)
Definition: byte_order.h:101
unsigned int u32
Definition: types.h:88
unsigned short u16
Definition: types.h:57
signed long i64
Definition: types.h:78
signed int i32
Definition: types.h:77
static u16 clib_byte_swap_u16(u16 x)
Definition: byte_order.h:57
signed short i16
Definition: types.h:46