blob: 7bc26002a2f6acc05dfd2c0bf8d19cd58b40f6e9 [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{
Damjan Marion97086e12020-05-16 01:48:47 +020059 return __builtin_bswap16 (x);
Dave Barachc3799992016-08-15 11:12:27 -040060}
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{
Damjan Marion97086e12020-05-16 01:48:47 +020071 return __builtin_bswap32 (x);
Ed Warnickecb9cada2015-12-08 15:45:58 -070072}
73
74always_inline i32
75clib_byte_swap_i32 (i32 x)
Dave Barachc3799992016-08-15 11:12:27 -040076{
77 return clib_byte_swap_u32 (x);
78}
Ed Warnickecb9cada2015-12-08 15:45:58 -070079
80always_inline u64
81clib_byte_swap_u64 (u64 x)
82{
Damjan Marion97086e12020-05-16 01:48:47 +020083 return __builtin_bswap64 (x);
Ed Warnickecb9cada2015-12-08 15:45:58 -070084}
85
86always_inline i64
87clib_byte_swap_i64 (i64 x)
Dave Barachc3799992016-08-15 11:12:27 -040088{
89 return clib_byte_swap_u64 (x);
90}
Ed Warnickecb9cada2015-12-08 15:45:58 -070091
92#define _(sex,type) \
93/* HOST -> SEX */ \
94always_inline type \
95clib_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 \
102always_inline type \
103clib_host_to_##sex##_mem_##type (type * x) \
104{ \
105 type v = x[0]; \
106 return clib_host_to_##sex##_##type (v); \
107} \
108 \
109always_inline type \
110clib_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 */ \
117always_inline type \
118clib_##sex##_to_host_##type (type x) \
119{ return clib_host_to_##sex##_##type (x); } \
120 \
121always_inline type \
122clib_##sex##_to_host_mem_##type (type * x) \
123{ return clib_host_to_##sex##_mem_##type (x); } \
124 \
125always_inline type \
126clib_##sex##_to_host_unaligned_mem_##type (type * x) \
127{ return clib_host_to_##sex##_unaligned_mem_##type (x); }
128
129#ifndef __cplusplus
Dave Barachc3799992016-08-15 11:12:27 -0400130_(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)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700137#endif
Ed Warnickecb9cada2015-12-08 15:45:58 -0700138#undef _
Ed Warnickecb9cada2015-12-08 15:45:58 -0700139/* Network "net" alias for "big". */
140#define _(type) \
141always_inline type \
142clib_net_to_host_##type (type x) \
143{ return clib_big_to_host_##type (x); } \
144 \
145always_inline type \
146clib_net_to_host_mem_##type (type * x) \
147{ return clib_big_to_host_mem_##type (x); } \
148 \
149always_inline type \
150clib_net_to_host_unaligned_mem_##type (type * x) \
151{ return clib_big_to_host_unaligned_mem_##type (x); } \
152 \
153always_inline type \
154clib_host_to_net_##type (type x) \
155{ return clib_host_to_big_##type (x); } \
156 \
157always_inline type \
158clib_host_to_net_mem_##type (type * x) \
159{ return clib_host_to_big_mem_##type (x); } \
160 \
161always_inline type \
162clib_host_to_net_unaligned_mem_##type (type * x) \
163{ return clib_host_to_big_unaligned_mem_##type (x); }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700164#ifndef __cplusplus
Dave Barachc3799992016-08-15 11:12:27 -0400165 _(u16);
166_(i16);
167_(u32);
168_(i32);
169_(u64);
170_(i64);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700171#endif
172
173#undef _
174
Dave Barachd0853d72019-06-28 15:08:49 -0400175/* Dummy endian swap functions for IEEE floating-point numbers */
Dave Barachd0853d72019-06-28 15:08:49 -0400176always_inline f64 clib_net_to_host_f64 (f64 x) { return x; }
177always_inline f64 clib_host_to_net_f64 (f64 x) { return x; }
178always_inline f32 clib_net_to_host_f32 (f32 x) { return x; }
179always_inline f32 clib_host_to_net_f32 (f32 x) { return x; }
Dave Barachd0853d72019-06-28 15:08:49 -0400180
Ed Warnickecb9cada2015-12-08 15:45:58 -0700181#endif /* included_clib_byte_order_h */
Dave Barachc3799992016-08-15 11:12:27 -0400182
183/*
184 * fd.io coding-style-patch-verification: ON
185 *
186 * Local Variables:
187 * eval: (c-set-style "gnu")
188 * End:
189 */