Initial commit of vpp code.
[vpp.git] / vppinfra / vppinfra / byte_order.h
1 /*
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
43 #if defined(__x86_64__) || defined(i386)
44 #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
56 always_inline u16
57 clib_byte_swap_u16 (u16 x)
58 { return (x >> 8) | (x << 8); }
59
60 always_inline i16
61 clib_byte_swap_i16 (i16 x)
62 { return clib_byte_swap_u16 (x); }
63
64 always_inline u32
65 clib_byte_swap_u32 (u32 x)
66 {
67 #if defined (i386) || defined (__x86_64__)
68   if (! __builtin_constant_p (x))
69     {
70       asm volatile ("bswap %0" : "=r" (x) : "0" (x));
71       return x;
72     }
73 #endif
74   return ((x << 24)
75           | ((x & 0xff00) << 8)
76           | ((x >> 8) & 0xff00)
77           | (x >> 24));
78 }
79
80 always_inline i32
81 clib_byte_swap_i32 (i32 x)
82 { return clib_byte_swap_u32 (x); }
83
84 always_inline u64
85 clib_byte_swap_u64 (u64 x)
86 {
87 #if defined (__x86_64__)
88   if (! __builtin_constant_p (x))
89     {
90       asm volatile ("bswapq %0" : "=r" (x) : "0" (x));
91       return x;
92     }
93 #endif
94 #define _(x,n,i) \
95   ((((x) >> (8*(i))) & 0xff) << (8*((n)-(i)-1)))
96   return (_ (x, 8, 0) | _ (x, 8, 1)
97           | _ (x, 8, 2) | _ (x, 8, 3)
98           | _ (x, 8, 4) | _ (x, 8, 5)
99           | _ (x, 8, 6) | _ (x, 8, 7));
100 #undef _
101 }
102
103 always_inline i64
104 clib_byte_swap_i64 (i64 x)
105 { return clib_byte_swap_u64 (x); }
106
107 #define _(sex,type)                                             \
108 /* HOST -> SEX */                                               \
109 always_inline type                                              \
110 clib_host_to_##sex##_##type (type x)                            \
111 {                                                               \
112   if (! clib_arch_is_##sex##_endian)                            \
113     x = clib_byte_swap_##type (x);                              \
114   return x;                                                     \
115 }                                                               \
116                                                                 \
117 always_inline type                                              \
118 clib_host_to_##sex##_mem_##type (type * x)                      \
119 {                                                               \
120   type v = x[0];                                                \
121   return clib_host_to_##sex##_##type (v);                       \
122 }                                                               \
123                                                                 \
124 always_inline type                                              \
125 clib_host_to_##sex##_unaligned_mem_##type (type * x)            \
126 {                                                               \
127   type v = clib_mem_unaligned (x, type);                        \
128   return clib_host_to_##sex##_##type (v);                       \
129 }                                                               \
130                                                                 \
131 /* SEX -> HOST */                                               \
132 always_inline type                                              \
133 clib_##sex##_to_host_##type (type x)                            \
134 { return clib_host_to_##sex##_##type (x); }                     \
135                                                                 \
136 always_inline type                                              \
137 clib_##sex##_to_host_mem_##type (type * x)                      \
138 { return clib_host_to_##sex##_mem_##type (x); }                 \
139                                                                 \
140 always_inline type                                              \
141 clib_##sex##_to_host_unaligned_mem_##type (type * x)            \
142 { return clib_host_to_##sex##_unaligned_mem_##type (x); }
143
144 #ifndef __cplusplus
145 _ (little, u16)
146 _ (little, u32)
147 _ (little, u64)
148 _ (little, i16)
149 _ (little, i32)
150 _ (little, i64)
151 _ (big, u16)
152 _ (big, u32)
153 _ (big, u64)
154 _ (big, i16)
155 _ (big, i32)
156 _ (big, i64)
157 #endif
158
159 #undef _
160
161 /* Network "net" alias for "big". */
162 #define _(type)                                         \
163 always_inline type                                      \
164 clib_net_to_host_##type (type x)                        \
165 { return clib_big_to_host_##type (x); }                 \
166                                                         \
167 always_inline type                                      \
168 clib_net_to_host_mem_##type (type * x)                  \
169 { return clib_big_to_host_mem_##type (x); }             \
170                                                         \
171 always_inline type                                      \
172 clib_net_to_host_unaligned_mem_##type (type * x)        \
173 { return clib_big_to_host_unaligned_mem_##type (x); }   \
174                                                         \
175 always_inline type                                      \
176 clib_host_to_net_##type (type x)                        \
177 { return clib_host_to_big_##type (x); }                 \
178                                                         \
179 always_inline type                                      \
180 clib_host_to_net_mem_##type (type * x)                  \
181 { return clib_host_to_big_mem_##type (x); }             \
182                                                         \
183 always_inline type                                      \
184 clib_host_to_net_unaligned_mem_##type (type * x)        \
185 { return clib_host_to_big_unaligned_mem_##type (x); }
186
187 #ifndef __cplusplus
188 _ (u16);
189 _ (i16);
190 _ (u32);
191 _ (i32);
192 _ (u64);
193 _ (i64);
194 #endif
195
196 #undef _
197
198 #endif /* included_clib_byte_order_h */