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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 Copyright (c) 2004 Eliot Dresselhaus
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:
26 The above copyright notice and this permission notice shall be
27 included in all copies or substantial portions of the Software.
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.
38 #ifndef included_clib_byte_order_h
39 #define included_clib_byte_order_h
41 #include <vppinfra/clib.h>
43 #if (__BYTE_ORDER__)==( __ORDER_LITTLE_ENDIAN__)
44 #define CLIB_ARCH_IS_BIG_ENDIAN (0)
45 #define CLIB_ARCH_IS_LITTLE_ENDIAN (1)
47 /* Default is big endian. */
48 #define CLIB_ARCH_IS_BIG_ENDIAN (1)
49 #define CLIB_ARCH_IS_LITTLE_ENDIAN (0)
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
57 clib_byte_swap_u16 (u16 x)
59 return __builtin_bswap16 (x);
63 clib_byte_swap_i16 (i16 x)
65 return clib_byte_swap_u16 (x);
69 clib_byte_swap_u32 (u32 x)
71 return __builtin_bswap32 (x);
75 clib_byte_swap_i32 (i32 x)
77 return clib_byte_swap_u32 (x);
81 clib_byte_swap_u64 (u64 x)
83 return __builtin_bswap64 (x);
87 clib_byte_swap_i64 (i64 x)
89 return clib_byte_swap_u64 (x);
95 clib_host_to_##sex##_##type (type x) \
97 if (! clib_arch_is_##sex##_endian) \
98 x = clib_byte_swap_##type (x); \
103 clib_host_to_##sex##_mem_##type (type * x) \
106 return clib_host_to_##sex##_##type (v); \
110 clib_host_to_##sex##_unaligned_mem_##type (type * x) \
112 type v = clib_mem_unaligned (x, type); \
113 return clib_host_to_##sex##_##type (v); \
118 clib_##sex##_to_host_##type (type x) \
119 { return clib_host_to_##sex##_##type (x); } \
122 clib_##sex##_to_host_mem_##type (type * x) \
123 { return clib_host_to_##sex##_mem_##type (x); } \
126 clib_##sex##_to_host_unaligned_mem_##type (type * x) \
127 { return clib_host_to_##sex##_unaligned_mem_##type (x); }
136 _(big, u16) _(big, u32) _(big, u64) _(big, i16) _(big, i32) _(big, i64)
139 /* Network "net" alias for "big". */
142 clib_net_to_host_##type (type x) \
143 { return clib_big_to_host_##type (x); } \
146 clib_net_to_host_mem_##type (type * x) \
147 { return clib_big_to_host_mem_##type (x); } \
150 clib_net_to_host_unaligned_mem_##type (type * x) \
151 { return clib_big_to_host_unaligned_mem_##type (x); } \
154 clib_host_to_net_##type (type x) \
155 { return clib_host_to_big_##type (x); } \
158 clib_host_to_net_mem_##type (type * x) \
159 { return clib_host_to_big_mem_##type (x); } \
162 clib_host_to_net_unaligned_mem_##type (type * x) \
163 { return clib_host_to_big_unaligned_mem_##type (x); }
175 /* Dummy endian swap functions for IEEE floating-point numbers */
177 always_inline f64 clib_net_to_host_f64 (f64 x) { return x; }
178 always_inline f64 clib_host_to_net_f64 (f64 x) { return x; }
179 always_inline f32 clib_net_to_host_f32 (f32 x) { return x; }
180 always_inline f32 clib_host_to_net_f32 (f32 x) { return x; }
183 #endif /* included_clib_byte_order_h */
186 * fd.io coding-style-patch-verification: ON
189 * eval: (c-set-style "gnu")