4902b48f7fc8b043b8029fbce177f7b51c6b0a24
[dpdk.git] / lib / librte_eal / common / include / rte_byteorder.h
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2013 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  * 
7  *   Redistribution and use in source and binary forms, with or without 
8  *   modification, are permitted provided that the following conditions 
9  *   are met:
10  * 
11  *     * Redistributions of source code must retain the above copyright 
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright 
14  *       notice, this list of conditions and the following disclaimer in 
15  *       the documentation and/or other materials provided with the 
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its 
18  *       contributors may be used to endorse or promote products derived 
19  *       from this software without specific prior written permission.
20  * 
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  * 
33  */
34
35 #ifndef _RTE_BYTEORDER_H_
36 #define _RTE_BYTEORDER_H_
37
38 /**
39  * @file
40  *
41  * Byte Swap Operations
42  *
43  * This file defines a generic API for byte swap operations. Part of
44  * the implementation is architecture-specific.
45  */
46
47 #ifdef __cplusplus
48 extern "C" {
49 #endif
50
51 #include <stdint.h>
52
53 /*
54  * An internal function to swap bytes in a 16-bit value.
55  *
56  * It is used by rte_bswap16() when the value is constant. Do not use
57  * this function directly; rte_bswap16() is preferred.
58  */
59 static inline uint16_t
60 rte_constant_bswap16(uint16_t x)
61 {
62         return (uint16_t)(((x & 0x00ffU) << 8) |
63                 ((x & 0xff00U) >> 8));
64 }
65
66 /*
67  * An internal function to swap bytes in a 32-bit value.
68  *
69  * It is used by rte_bswap32() when the value is constant. Do not use
70  * this function directly; rte_bswap32() is preferred.
71  */
72 static inline uint32_t
73 rte_constant_bswap32(uint32_t x)
74 {
75         return  ((x & 0x000000ffUL) << 24) |
76                 ((x & 0x0000ff00UL) << 8) |
77                 ((x & 0x00ff0000UL) >> 8) |
78                 ((x & 0xff000000UL) >> 24);
79 }
80
81 /*
82  * An internal function to swap bytes of a 64-bit value.
83  *
84  * It is used by rte_bswap64() when the value is constant. Do not use
85  * this function directly; rte_bswap64() is preferred.
86  */
87 static inline uint64_t
88 rte_constant_bswap64(uint64_t x)
89 {
90         return  ((x & 0x00000000000000ffULL) << 56) |
91                 ((x & 0x000000000000ff00ULL) << 40) |
92                 ((x & 0x0000000000ff0000ULL) << 24) |
93                 ((x & 0x00000000ff000000ULL) <<  8) |
94                 ((x & 0x000000ff00000000ULL) >>  8) |
95                 ((x & 0x0000ff0000000000ULL) >> 24) |
96                 ((x & 0x00ff000000000000ULL) >> 40) |
97                 ((x & 0xff00000000000000ULL) >> 56);
98 }
99
100 /*
101  * An architecture-optimized byte swap for a 16-bit value.
102  *
103  * Do not use this function directly. The preferred function is rte_bswap16().
104  */
105 static inline uint16_t rte_arch_bswap16(uint16_t _x)
106 {
107         register uint16_t x = _x;
108         asm volatile ("xchgb %b[x1],%h[x2]"
109                       : [x1] "=Q" (x)
110                       : [x2] "0" (x)
111                       );
112         return x;
113 }
114
115 /*
116  * An architecture-optimized byte swap for a 32-bit value.
117  *
118  * Do not use this function directly. The preferred function is rte_bswap32().
119  */
120 static inline uint32_t rte_arch_bswap32(uint32_t _x)
121 {
122         register uint32_t x = _x;
123         asm volatile ("bswap %[x]"
124                       : [x] "+r" (x)
125                       );
126         return x;
127 }
128
129 /*
130  * An architecture-optimized byte swap for a 64-bit value.
131  *
132   * Do not use this function directly. The preferred function is rte_bswap64().
133  */
134 #ifdef RTE_ARCH_X86_64
135 /* 64-bit mode */
136 static inline uint64_t rte_arch_bswap64(uint64_t _x)
137 {
138         register uint64_t x = _x;
139         asm volatile ("bswap %[x]"
140                       : [x] "+r" (x)
141                       );
142         return x;
143 }
144 #else /* ! RTE_ARCH_X86_64 */
145 /* Compat./Leg. mode */
146 static inline uint64_t rte_arch_bswap64(uint64_t x)
147 {
148         uint64_t ret = 0;
149         ret |= ((uint64_t)rte_arch_bswap32(x & 0xffffffffUL) << 32);
150         ret |= ((uint64_t)rte_arch_bswap32((x >> 32) & 0xffffffffUL));
151         return ret;
152 }
153 #endif /* RTE_ARCH_X86_64 */
154
155
156 /**
157  * Swap bytes in a 16-bit value.
158  */
159 #define rte_bswap16(x) ((uint16_t)(__builtin_constant_p(x) ?            \
160                                    rte_constant_bswap16(x) :            \
161                                    rte_arch_bswap16(x)))
162
163 #ifndef RTE_FORCE_INTRINSICS
164 /**
165  * Swap bytes in a 32-bit value.
166  */
167 #define rte_bswap32(x) ((uint32_t)(__builtin_constant_p(x) ?            \
168                                    rte_constant_bswap32(x) :            \
169                                    rte_arch_bswap32(x)))
170
171 /**
172  * Swap bytes in a 64-bit value.
173  */
174 #define rte_bswap64(x) ((uint64_t)(__builtin_constant_p(x) ?            \
175                                    rte_constant_bswap64(x) :            \
176                                    rte_arch_bswap64(x)))
177
178 #else
179
180 /* __builtin_bswap16 is only available gcc 4.8 and upwards */
181
182 /**
183  * Swap bytes in a 32-bit value.
184  */
185 #define rte_bswap32(x) __builtin_bswap32(x)
186
187 /**
188  * Swap bytes in a 64-bit value.
189  */
190 #define rte_bswap64(x) __builtin_bswap64(x)
191
192 #endif
193
194 /**
195  * Convert a 16-bit value from CPU order to little endian.
196  */
197 #define rte_cpu_to_le_16(x) (x)
198
199 /**
200  * Convert a 32-bit value from CPU order to little endian.
201  */
202 #define rte_cpu_to_le_32(x) (x)
203
204 /**
205  * Convert a 64-bit value from CPU order to little endian.
206  */
207 #define rte_cpu_to_le_64(x) (x)
208
209
210 /**
211  * Convert a 16-bit value from CPU order to big endian.
212  */
213 #define rte_cpu_to_be_16(x) rte_bswap16(x)
214
215 /**
216  * Convert a 32-bit value from CPU order to big endian.
217  */
218 #define rte_cpu_to_be_32(x) rte_bswap32(x)
219
220 /**
221  * Convert a 64-bit value from CPU order to big endian.
222  */
223 #define rte_cpu_to_be_64(x) rte_bswap64(x)
224
225
226 /**
227  * Convert a 16-bit value from little endian to CPU order.
228  */
229 #define rte_le_to_cpu_16(x) (x)
230
231 /**
232  * Convert a 32-bit value from little endian to CPU order.
233  */
234 #define rte_le_to_cpu_32(x) (x)
235
236 /**
237  * Convert a 64-bit value from little endian to CPU order.
238  */
239 #define rte_le_to_cpu_64(x) (x)
240
241
242 /**
243  * Convert a 16-bit value from big endian to CPU order.
244  */
245 #define rte_be_to_cpu_16(x) rte_bswap16(x)
246
247 /**
248  * Convert a 32-bit value from big endian to CPU order.
249  */
250 #define rte_be_to_cpu_32(x) rte_bswap32(x)
251
252 /**
253  * Convert a 64-bit value from big endian to CPU order.
254  */
255 #define rte_be_to_cpu_64(x) rte_bswap64(x)
256
257 #ifdef __cplusplus
258 }
259 #endif
260
261 #endif /* _RTE_BYTEORDER_H_ */