blob: b263538c6fe4791d39c2b8c6076b338c27410f12 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
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
Christophe Fontainefef15b42016-04-09 12:38:49 +090043#if (__BYTE_ORDER__)==( __ORDER_LITTLE_ENDIAN__)
Ed Warnickecb9cada2015-12-08 15:45:58 -070044#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
56always_inline u16
57clib_byte_swap_u16 (u16 x)
Dave Barachc3799992016-08-15 11:12:27 -040058{
59 return (x >> 8) | (x << 8);
60}
Ed Warnickecb9cada2015-12-08 15:45:58 -070061
62always_inline i16
63clib_byte_swap_i16 (i16 x)
Dave Barachc3799992016-08-15 11:12:27 -040064{
65 return clib_byte_swap_u16 (x);
66}
Ed Warnickecb9cada2015-12-08 15:45:58 -070067
68always_inline u32
69clib_byte_swap_u32 (u32 x)
70{
71#if defined (i386) || defined (__x86_64__)
Dave Barachc3799992016-08-15 11:12:27 -040072 if (!__builtin_constant_p (x))
Ed Warnickecb9cada2015-12-08 15:45:58 -070073 {
Dave Barachc3799992016-08-15 11:12:27 -040074 asm volatile ("bswap %0":"=r" (x):"0" (x));
Ed Warnickecb9cada2015-12-08 15:45:58 -070075 return x;
76 }
77#endif
Dave Barachc3799992016-08-15 11:12:27 -040078 return ((x << 24) | ((x & 0xff00) << 8) | ((x >> 8) & 0xff00) | (x >> 24));
Ed Warnickecb9cada2015-12-08 15:45:58 -070079}
80
81always_inline i32
82clib_byte_swap_i32 (i32 x)
Dave Barachc3799992016-08-15 11:12:27 -040083{
84 return clib_byte_swap_u32 (x);
85}
Ed Warnickecb9cada2015-12-08 15:45:58 -070086
87always_inline u64
88clib_byte_swap_u64 (u64 x)
89{
90#if defined (__x86_64__)
Dave Barachc3799992016-08-15 11:12:27 -040091 if (!__builtin_constant_p (x))
Ed Warnickecb9cada2015-12-08 15:45:58 -070092 {
Dave Barachc3799992016-08-15 11:12:27 -040093 asm volatile ("bswapq %0":"=r" (x):"0" (x));
Ed Warnickecb9cada2015-12-08 15:45:58 -070094 return x;
95 }
96#endif
97#define _(x,n,i) \
98 ((((x) >> (8*(i))) & 0xff) << (8*((n)-(i)-1)))
Dave Barachc3799992016-08-15 11:12:27 -040099 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));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700102#undef _
103}
104
105always_inline i64
106clib_byte_swap_i64 (i64 x)
Dave Barachc3799992016-08-15 11:12:27 -0400107{
108 return clib_byte_swap_u64 (x);
109}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700110
111#define _(sex,type) \
112/* HOST -> SEX */ \
113always_inline type \
114clib_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 \
121always_inline type \
122clib_host_to_##sex##_mem_##type (type * x) \
123{ \
124 type v = x[0]; \
125 return clib_host_to_##sex##_##type (v); \
126} \
127 \
128always_inline type \
129clib_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 */ \
136always_inline type \
137clib_##sex##_to_host_##type (type x) \
138{ return clib_host_to_##sex##_##type (x); } \
139 \
140always_inline type \
141clib_##sex##_to_host_mem_##type (type * x) \
142{ return clib_host_to_##sex##_mem_##type (x); } \
143 \
144always_inline type \
145clib_##sex##_to_host_unaligned_mem_##type (type * x) \
146{ return clib_host_to_##sex##_unaligned_mem_##type (x); }
147
148#ifndef __cplusplus
Dave Barachc3799992016-08-15 11:12:27 -0400149_(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)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700156#endif
Ed Warnickecb9cada2015-12-08 15:45:58 -0700157#undef _
Ed Warnickecb9cada2015-12-08 15:45:58 -0700158/* Network "net" alias for "big". */
159#define _(type) \
160always_inline type \
161clib_net_to_host_##type (type x) \
162{ return clib_big_to_host_##type (x); } \
163 \
164always_inline type \
165clib_net_to_host_mem_##type (type * x) \
166{ return clib_big_to_host_mem_##type (x); } \
167 \
168always_inline type \
169clib_net_to_host_unaligned_mem_##type (type * x) \
170{ return clib_big_to_host_unaligned_mem_##type (x); } \
171 \
172always_inline type \
173clib_host_to_net_##type (type x) \
174{ return clib_host_to_big_##type (x); } \
175 \
176always_inline type \
177clib_host_to_net_mem_##type (type * x) \
178{ return clib_host_to_big_mem_##type (x); } \
179 \
180always_inline type \
181clib_host_to_net_unaligned_mem_##type (type * x) \
182{ return clib_host_to_big_unaligned_mem_##type (x); }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700183#ifndef __cplusplus
Dave Barachc3799992016-08-15 11:12:27 -0400184 _(u16);
185_(i16);
186_(u32);
187_(i32);
188_(u64);
189_(i64);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700190#endif
191
192#undef _
193
194#endif /* included_clib_byte_order_h */
Dave Barachc3799992016-08-15 11:12:27 -0400195
196/*
197 * fd.io coding-style-patch-verification: ON
198 *
199 * Local Variables:
200 * eval: (c-set-style "gnu")
201 * End:
202 */