FD.io VPP  v21.10.1-2-g0a485f517
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 __builtin_bswap16 (x);
60 }
61 
64 {
65  return clib_byte_swap_u16 (x);
66 }
67 
70 {
71  return __builtin_bswap32 (x);
72 }
73 
76 {
77  return clib_byte_swap_u32 (x);
78 }
79 
82 {
83  return __builtin_bswap64 (x);
84 }
85 
88 {
89  return clib_byte_swap_u64 (x);
90 }
91 
92 #define _(sex,type) \
93 /* HOST -> SEX */ \
94 always_inline type \
95 clib_host_to_##sex##_##type (type x) \
96 { \
97  if (! clib_arch_is_##sex##_endian) \
98  x = clib_byte_swap_##type (x); \
99  return x; \
100 } \
101  \
102 always_inline type \
103 clib_host_to_##sex##_mem_##type (type * x) \
104 { \
105  type v = x[0]; \
106  return clib_host_to_##sex##_##type (v); \
107 } \
108  \
109 always_inline type \
110 clib_host_to_##sex##_unaligned_mem_##type (type * x) \
111 { \
112  type v = clib_mem_unaligned (x, type); \
113  return clib_host_to_##sex##_##type (v); \
114 } \
115  \
116 /* SEX -> HOST */ \
117 always_inline type \
118 clib_##sex##_to_host_##type (type x) \
119 { return clib_host_to_##sex##_##type (x); } \
120  \
121 always_inline type \
122 clib_##sex##_to_host_mem_##type (type * x) \
123 { return clib_host_to_##sex##_mem_##type (x); } \
124  \
125 always_inline type \
126 clib_##sex##_to_host_unaligned_mem_##type (type * x) \
127 { return clib_host_to_##sex##_unaligned_mem_##type (x); }
128 
129 #ifndef __cplusplus
130 _(little, u16)
131 _(little, u32)
132 _(little, u64)
133 _(little, i16)
134 _(little, i32)
135 _(little, i64)
136 _(big, u16) _(big, u32) _(big, u64) _(big, i16) _(big, i32) _(big, i64)
137 #endif
138 #undef _
139 /* Network "net" alias for "big". */
140 #define _(type) \
141 always_inline type \
142 clib_net_to_host_##type (type x) \
143 { return clib_big_to_host_##type (x); } \
144  \
145 always_inline type \
146 clib_net_to_host_mem_##type (type * x) \
147 { return clib_big_to_host_mem_##type (x); } \
148  \
149 always_inline type \
150 clib_net_to_host_unaligned_mem_##type (type * x) \
151 { return clib_big_to_host_unaligned_mem_##type (x); } \
152  \
153 always_inline type \
154 clib_host_to_net_##type (type x) \
155 { return clib_host_to_big_##type (x); } \
156  \
157 always_inline type \
158 clib_host_to_net_mem_##type (type * x) \
159 { return clib_host_to_big_mem_##type (x); } \
160  \
161 always_inline type \
162 clib_host_to_net_unaligned_mem_##type (type * x) \
163 { return clib_host_to_big_unaligned_mem_##type (x); }
164 #ifndef __cplusplus
165  _(u16);
166 _(i16);
167 _(u32);
168 _(i32);
169 _(u64);
170 _(i64);
171 #endif
172 
173 #undef _
174 
175 /* Dummy endian swap functions for IEEE floating-point numbers */
176 /* *INDENT-OFF* */
181 /* *INDENT-ON* */
182 
183 #endif /* included_clib_byte_order_h */
184 
185 /*
186  * fd.io coding-style-patch-verification: ON
187  *
188  * Local Variables:
189  * eval: (c-set-style "gnu")
190  * End:
191  */
clib_byte_swap_u16
static u16 clib_byte_swap_u16(u16 x)
Definition: byte_order.h:57
clib_host_to_net_f64
static f64 clib_host_to_net_f64(f64 x)
Definition: byte_order.h:178
clib.h
u16
unsigned short u16
Definition: types.h:57
f32
float f32
Definition: types.h:143
i64
signed long i64
Definition: types.h:78
clib_net_to_host_f32
static f32 clib_net_to_host_f32(f32 x)
Definition: byte_order.h:179
i32
signed int i32
Definition: types.h:77
i16
signed short i16
Definition: types.h:46
clib_byte_swap_u32
static u32 clib_byte_swap_u32(u32 x)
Definition: byte_order.h:69
f64
double f64
Definition: types.h:142
clib_host_to_net_f32
static f32 clib_host_to_net_f32(f32 x)
Definition: byte_order.h:180
always_inline
#define always_inline
Definition: rdma_mlx5dv.h:23
u64
unsigned long u64
Definition: types.h:89
u32
unsigned int u32
Definition: types.h:88
clib_net_to_host_f64
static f64 clib_net_to_host_f64(f64 x)
Definition: byte_order.h:177
clib_byte_swap_i64
static i64 clib_byte_swap_i64(i64 x)
Definition: byte_order.h:87
clib_byte_swap_i32
static i32 clib_byte_swap_i32(i32 x)
Definition: byte_order.h:75
clib_byte_swap_i16
static i16 clib_byte_swap_i16(i16 x)
Definition: byte_order.h:63
clib_byte_swap_u64
static u64 clib_byte_swap_u64(u64 x)
Definition: byte_order.h:81