1cb9cadadSEd Warnicke/*
2cb9cadadSEd Warnicke * Copyright (c) 2015 Cisco and/or its affiliates.
3cb9cadadSEd Warnicke * Licensed under the Apache License, Version 2.0 (the "License");
4cb9cadadSEd Warnicke * you may not use this file except in compliance with the License.
5cb9cadadSEd Warnicke * You may obtain a copy of the License at:
6cb9cadadSEd Warnicke *
7cb9cadadSEd Warnicke *     http://www.apache.org/licenses/LICENSE-2.0
8cb9cadadSEd Warnicke *
9cb9cadadSEd Warnicke * Unless required by applicable law or agreed to in writing, software
10cb9cadadSEd Warnicke * distributed under the License is distributed on an "AS IS" BASIS,
11cb9cadadSEd Warnicke * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12cb9cadadSEd Warnicke * See the License for the specific language governing permissions and
13cb9cadadSEd Warnicke * limitations under the License.
14cb9cadadSEd Warnicke */
15cb9cadadSEd Warnicke/*
16cb9cadadSEd Warnicke  Copyright (c) 2004 Eliot Dresselhaus
17cb9cadadSEd Warnicke
18cb9cadadSEd Warnicke  Permission is hereby granted, free of charge, to any person obtaining
19cb9cadadSEd Warnicke  a copy of this software and associated documentation files (the
20cb9cadadSEd Warnicke  "Software"), to deal in the Software without restriction, including
21cb9cadadSEd Warnicke  without limitation the rights to use, copy, modify, merge, publish,
22cb9cadadSEd Warnicke  distribute, sublicense, and/or sell copies of the Software, and to
23cb9cadadSEd Warnicke  permit persons to whom the Software is furnished to do so, subject to
24cb9cadadSEd Warnicke  the following conditions:
25cb9cadadSEd Warnicke
26cb9cadadSEd Warnicke  The above copyright notice and this permission notice shall be
27cb9cadadSEd Warnicke  included in all copies or substantial portions of the Software.
28cb9cadadSEd Warnicke
29cb9cadadSEd Warnicke  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
30cb9cadadSEd Warnicke  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31cb9cadadSEd Warnicke  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
32cb9cadadSEd Warnicke  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
33cb9cadadSEd Warnicke  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
34cb9cadadSEd Warnicke  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
35cb9cadadSEd Warnicke  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36cb9cadadSEd Warnicke*/
37cb9cadadSEd Warnicke
38cb9cadadSEd Warnicke#ifndef included_clib_byte_order_h
39cb9cadadSEd Warnicke#define included_clib_byte_order_h
40cb9cadadSEd Warnicke
41cb9cadadSEd Warnicke#include <vppinfra/clib.h>
42cb9cadadSEd Warnicke
43fef15b4bSChristophe Fontaine#if (__BYTE_ORDER__)==( __ORDER_LITTLE_ENDIAN__)
44cb9cadadSEd Warnicke#define CLIB_ARCH_IS_BIG_ENDIAN (0)
45cb9cadadSEd Warnicke#define CLIB_ARCH_IS_LITTLE_ENDIAN (1)
46cb9cadadSEd Warnicke#else
47cb9cadadSEd Warnicke/* Default is big endian. */
48cb9cadadSEd Warnicke#define CLIB_ARCH_IS_BIG_ENDIAN (1)
49cb9cadadSEd Warnicke#define CLIB_ARCH_IS_LITTLE_ENDIAN (0)
50cb9cadadSEd Warnicke#endif
51cb9cadadSEd Warnicke
52cb9cadadSEd Warnicke/* Big/little endian. */
53cb9cadadSEd Warnicke#define clib_arch_is_big_endian    CLIB_ARCH_IS_BIG_ENDIAN
54cb9cadadSEd Warnicke#define clib_arch_is_little_endian CLIB_ARCH_IS_LITTLE_ENDIAN
55cb9cadadSEd Warnicke
56cb9cadadSEd Warnickealways_inline u16
57cb9cadadSEd Warnickeclib_byte_swap_u16 (u16 x)
58c3799996SDave Barach{
5997086e18SDamjan Marion  return __builtin_bswap16 (x);
60c3799996SDave Barach}
61cb9cadadSEd Warnicke
62cb9cadadSEd Warnickealways_inline i16
63cb9cadadSEd Warnickeclib_byte_swap_i16 (i16 x)
64c3799996SDave Barach{
65c3799996SDave Barach  return clib_byte_swap_u16 (x);
66c3799996SDave Barach}
67cb9cadadSEd Warnicke
68cb9cadadSEd Warnickealways_inline u32
69cb9cadadSEd Warnickeclib_byte_swap_u32 (u32 x)
70cb9cadadSEd Warnicke{
7197086e18SDamjan Marion  return __builtin_bswap32 (x);
72cb9cadadSEd Warnicke}
73cb9cadadSEd Warnicke
74cb9cadadSEd Warnickealways_inline i32
75cb9cadadSEd Warnickeclib_byte_swap_i32 (i32 x)
76c3799996SDave Barach{
77c3799996SDave Barach  return clib_byte_swap_u32 (x);
78c3799996SDave Barach}
79cb9cadadSEd Warnicke
80cb9cadadSEd Warnickealways_inline u64
81cb9cadadSEd Warnickeclib_byte_swap_u64 (u64 x)
82cb9cadadSEd Warnicke{
8397086e18SDamjan Marion  return __builtin_bswap64 (x);
84cb9cadadSEd Warnicke}
85cb9cadadSEd Warnicke
86cb9cadadSEd Warnickealways_inline i64
87cb9cadadSEd Warnickeclib_byte_swap_i64 (i64 x)
88c3799996SDave Barach{
89c3799996SDave Barach  return clib_byte_swap_u64 (x);
90c3799996SDave Barach}
91cb9cadadSEd Warnicke
92cb9cadadSEd Warnicke#define _(sex,type)						\
93cb9cadadSEd Warnicke/* HOST -> SEX */						\
94cb9cadadSEd Warnickealways_inline type						\
95cb9cadadSEd Warnickeclib_host_to_##sex##_##type (type x)				\
96cb9cadadSEd Warnicke{								\
97cb9cadadSEd Warnicke  if (! clib_arch_is_##sex##_endian)				\
98cb9cadadSEd Warnicke    x = clib_byte_swap_##type (x);				\
99cb9cadadSEd Warnicke  return x;							\
100cb9cadadSEd Warnicke}								\
101cb9cadadSEd Warnicke								\
102cb9cadadSEd Warnickealways_inline type						\
103cb9cadadSEd Warnickeclib_host_to_##sex##_mem_##type (type * x)			\
104cb9cadadSEd Warnicke{								\
105cb9cadadSEd Warnicke  type v = x[0];						\
106cb9cadadSEd Warnicke  return clib_host_to_##sex##_##type (v);			\
107cb9cadadSEd Warnicke}								\
108cb9cadadSEd Warnicke								\
109cb9cadadSEd Warnickealways_inline type						\
110cb9cadadSEd Warnickeclib_host_to_##sex##_unaligned_mem_##type (type * x)		\
111cb9cadadSEd Warnicke{								\
112cb9cadadSEd Warnicke  type v = clib_mem_unaligned (x, type);			\
113cb9cadadSEd Warnicke  return clib_host_to_##sex##_##type (v);			\
114cb9cadadSEd Warnicke}								\
115cb9cadadSEd Warnicke								\
116cb9cadadSEd Warnicke/* SEX -> HOST */						\
117cb9cadadSEd Warnickealways_inline type						\
118cb9cadadSEd Warnickeclib_##sex##_to_host_##type (type x)				\
119cb9cadadSEd Warnicke{ return clib_host_to_##sex##_##type (x); }			\
120cb9cadadSEd Warnicke								\
121cb9cadadSEd Warnickealways_inline type						\
122cb9cadadSEd Warnickeclib_##sex##_to_host_mem_##type (type * x)			\
123cb9cadadSEd Warnicke{ return clib_host_to_##sex##_mem_##type (x); }			\
124cb9cadadSEd Warnicke								\
125cb9cadadSEd Warnickealways_inline type						\
126cb9cadadSEd Warnickeclib_##sex##_to_host_unaligned_mem_##type (type * x)		\
127cb9cadadSEd Warnicke{ return clib_host_to_##sex##_unaligned_mem_##type (x); }
128cb9cadadSEd Warnicke
129cb9cadadSEd Warnicke#ifndef __cplusplus
130c3799996SDave Barach_(little, u16)
131c3799996SDave Barach_(little, u32)
132c3799996SDave Barach_(little, u64)
133c3799996SDave Barach_(little, i16)
134c3799996SDave Barach_(little, i32)
135c3799996SDave Barach_(little, i64)
136c3799996SDave Barach_(big, u16) _(big, u32) _(big, u64) _(big, i16) _(big, i32) _(big, i64)
137cb9cadadSEd Warnicke#endif
138cb9cadadSEd Warnicke#undef _
139cb9cadadSEd Warnicke/* Network "net" alias for "big". */
140cb9cadadSEd Warnicke#define _(type)						\
141cb9cadadSEd Warnickealways_inline type					\
142cb9cadadSEd Warnickeclib_net_to_host_##type (type x)			\
143cb9cadadSEd Warnicke{ return clib_big_to_host_##type (x); }			\
144cb9cadadSEd Warnicke							\
145cb9cadadSEd Warnickealways_inline type					\
146cb9cadadSEd Warnickeclib_net_to_host_mem_##type (type * x)			\
147cb9cadadSEd Warnicke{ return clib_big_to_host_mem_##type (x); }		\
148cb9cadadSEd Warnicke							\
149cb9cadadSEd Warnickealways_inline type					\
150cb9cadadSEd Warnickeclib_net_to_host_unaligned_mem_##type (type * x)	\
151cb9cadadSEd Warnicke{ return clib_big_to_host_unaligned_mem_##type (x); }	\
152cb9cadadSEd Warnicke							\
153cb9cadadSEd Warnickealways_inline type					\
154cb9cadadSEd Warnickeclib_host_to_net_##type (type x)			\
155cb9cadadSEd Warnicke{ return clib_host_to_big_##type (x); }			\
156cb9cadadSEd Warnicke							\
157cb9cadadSEd Warnickealways_inline type					\
158cb9cadadSEd Warnickeclib_host_to_net_mem_##type (type * x)			\
159cb9cadadSEd Warnicke{ return clib_host_to_big_mem_##type (x); }		\
160cb9cadadSEd Warnicke							\
161cb9cadadSEd Warnickealways_inline type					\
162cb9cadadSEd Warnickeclib_host_to_net_unaligned_mem_##type (type * x)	\
163cb9cadadSEd Warnicke{ return clib_host_to_big_unaligned_mem_##type (x); }
164cb9cadadSEd Warnicke#ifndef __cplusplus
165c3799996SDave Barach  _(u16);
166c3799996SDave Barach_(i16);
167c3799996SDave Barach_(u32);
168c3799996SDave Barach_(i32);
169c3799996SDave Barach_(u64);
170c3799996SDave Barach_(i64);
171cb9cadadSEd Warnicke#endif
172cb9cadadSEd Warnicke
173cb9cadadSEd Warnicke#undef _
174cb9cadadSEd Warnicke
175d0853d71SDave Barach/* Dummy endian swap functions for IEEE floating-point numbers */
176d0853d71SDave Barach/* *INDENT-OFF* */
177d0853d71SDave Barachalways_inline f64 clib_net_to_host_f64 (f64 x) { return x; }
178d0853d71SDave Barachalways_inline f64 clib_host_to_net_f64 (f64 x) { return x; }
179d0853d71SDave Barachalways_inline f32 clib_net_to_host_f32 (f32 x) { return x; }
180d0853d71SDave Barachalways_inline f32 clib_host_to_net_f32 (f32 x) { return x; }
181d0853d71SDave Barach/* *INDENT-ON* */
182d0853d71SDave Barach
183cb9cadadSEd Warnicke#endif /* included_clib_byte_order_h */
184c3799996SDave Barach
185c3799996SDave Barach/*
186c3799996SDave Barach * fd.io coding-style-patch-verification: ON
187c3799996SDave Barach *
188c3799996SDave Barach * Local Variables:
189c3799996SDave Barach * eval: (c-set-style "gnu")
190c3799996SDave Barach * End:
191c3799996SDave Barach */
192