blob: 87918f14e5c0a1a644e06364fe118608efdb22da [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{
Gabriel Ganne70d44d32017-11-21 11:33:45 +010059#if defined (__aarch64__)
60 if (!__builtin_constant_p (x))
61 {
62 __asm__ ("rev16 %w0, %w0":"+r" (x));
63 return x;
64 }
65#endif
Dave Barachc3799992016-08-15 11:12:27 -040066 return (x >> 8) | (x << 8);
67}
Ed Warnickecb9cada2015-12-08 15:45:58 -070068
69always_inline i16
70clib_byte_swap_i16 (i16 x)
Dave Barachc3799992016-08-15 11:12:27 -040071{
72 return clib_byte_swap_u16 (x);
73}
Ed Warnickecb9cada2015-12-08 15:45:58 -070074
75always_inline u32
76clib_byte_swap_u32 (u32 x)
77{
78#if defined (i386) || defined (__x86_64__)
Dave Barachc3799992016-08-15 11:12:27 -040079 if (!__builtin_constant_p (x))
Ed Warnickecb9cada2015-12-08 15:45:58 -070080 {
Dave Barachc3799992016-08-15 11:12:27 -040081 asm volatile ("bswap %0":"=r" (x):"0" (x));
Ed Warnickecb9cada2015-12-08 15:45:58 -070082 return x;
83 }
Gabriel Ganne70d44d32017-11-21 11:33:45 +010084#elif defined (__aarch64__)
85 if (!__builtin_constant_p (x))
86 {
87 __asm__ ("rev %w0, %w0":"+r" (x));
88 return x;
89 }
Ed Warnickecb9cada2015-12-08 15:45:58 -070090#endif
Dave Barachc3799992016-08-15 11:12:27 -040091 return ((x << 24) | ((x & 0xff00) << 8) | ((x >> 8) & 0xff00) | (x >> 24));
Ed Warnickecb9cada2015-12-08 15:45:58 -070092}
93
94always_inline i32
95clib_byte_swap_i32 (i32 x)
Dave Barachc3799992016-08-15 11:12:27 -040096{
97 return clib_byte_swap_u32 (x);
98}
Ed Warnickecb9cada2015-12-08 15:45:58 -070099
100always_inline u64
101clib_byte_swap_u64 (u64 x)
102{
103#if defined (__x86_64__)
Dave Barachc3799992016-08-15 11:12:27 -0400104 if (!__builtin_constant_p (x))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700105 {
Dave Barachc3799992016-08-15 11:12:27 -0400106 asm volatile ("bswapq %0":"=r" (x):"0" (x));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700107 return x;
108 }
Gabriel Ganne70d44d32017-11-21 11:33:45 +0100109#elif defined (__aarch64__)
110 if (!__builtin_constant_p (x))
111 {
112 __asm__ ("rev %0, %0":"+r" (x));
113 return x;
114 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700115#endif
116#define _(x,n,i) \
117 ((((x) >> (8*(i))) & 0xff) << (8*((n)-(i)-1)))
Dave Barachc3799992016-08-15 11:12:27 -0400118 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));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700121#undef _
122}
123
124always_inline i64
125clib_byte_swap_i64 (i64 x)
Dave Barachc3799992016-08-15 11:12:27 -0400126{
127 return clib_byte_swap_u64 (x);
128}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700129
130#define _(sex,type) \
131/* HOST -> SEX */ \
132always_inline type \
133clib_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 \
140always_inline type \
141clib_host_to_##sex##_mem_##type (type * x) \
142{ \
143 type v = x[0]; \
144 return clib_host_to_##sex##_##type (v); \
145} \
146 \
147always_inline type \
148clib_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 */ \
155always_inline type \
156clib_##sex##_to_host_##type (type x) \
157{ return clib_host_to_##sex##_##type (x); } \
158 \
159always_inline type \
160clib_##sex##_to_host_mem_##type (type * x) \
161{ return clib_host_to_##sex##_mem_##type (x); } \
162 \
163always_inline type \
164clib_##sex##_to_host_unaligned_mem_##type (type * x) \
165{ return clib_host_to_##sex##_unaligned_mem_##type (x); }
166
167#ifndef __cplusplus
Dave Barachc3799992016-08-15 11:12:27 -0400168_(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)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700175#endif
Ed Warnickecb9cada2015-12-08 15:45:58 -0700176#undef _
Ed Warnickecb9cada2015-12-08 15:45:58 -0700177/* Network "net" alias for "big". */
178#define _(type) \
179always_inline type \
180clib_net_to_host_##type (type x) \
181{ return clib_big_to_host_##type (x); } \
182 \
183always_inline type \
184clib_net_to_host_mem_##type (type * x) \
185{ return clib_big_to_host_mem_##type (x); } \
186 \
187always_inline type \
188clib_net_to_host_unaligned_mem_##type (type * x) \
189{ return clib_big_to_host_unaligned_mem_##type (x); } \
190 \
191always_inline type \
192clib_host_to_net_##type (type x) \
193{ return clib_host_to_big_##type (x); } \
194 \
195always_inline type \
196clib_host_to_net_mem_##type (type * x) \
197{ return clib_host_to_big_mem_##type (x); } \
198 \
199always_inline type \
200clib_host_to_net_unaligned_mem_##type (type * x) \
201{ return clib_host_to_big_unaligned_mem_##type (x); }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700202#ifndef __cplusplus
Dave Barachc3799992016-08-15 11:12:27 -0400203 _(u16);
204_(i16);
205_(u32);
206_(i32);
207_(u64);
208_(i64);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700209#endif
210
211#undef _
212
Dave Barachd0853d72019-06-28 15:08:49 -0400213/* Dummy endian swap functions for IEEE floating-point numbers */
214/* *INDENT-OFF* */
215always_inline f64 clib_net_to_host_f64 (f64 x) { return x; }
216always_inline f64 clib_host_to_net_f64 (f64 x) { return x; }
217always_inline f32 clib_net_to_host_f32 (f32 x) { return x; }
218always_inline f32 clib_host_to_net_f32 (f32 x) { return x; }
219/* *INDENT-ON* */
220
Ed Warnickecb9cada2015-12-08 15:45:58 -0700221#endif /* included_clib_byte_order_h */
Dave Barachc3799992016-08-15 11:12:27 -0400222
223/*
224 * fd.io coding-style-patch-verification: ON
225 *
226 * Local Variables:
227 * eval: (c-set-style "gnu")
228 * End:
229 */