update Intel copyright years to 2014
[dpdk.git] / lib / librte_eal / common / include / rte_byteorder.h
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2014 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 #ifndef _RTE_BYTEORDER_H_
35 #define _RTE_BYTEORDER_H_
36
37 /**
38  * @file
39  *
40  * Byte Swap Operations
41  *
42  * This file defines a generic API for byte swap operations. Part of
43  * the implementation is architecture-specific.
44  */
45
46 #ifdef __cplusplus
47 extern "C" {
48 #endif
49
50 #include <stdint.h>
51
52 /*
53  * An internal function to swap bytes in a 16-bit value.
54  *
55  * It is used by rte_bswap16() when the value is constant. Do not use
56  * this function directly; rte_bswap16() is preferred.
57  */
58 static inline uint16_t
59 rte_constant_bswap16(uint16_t x)
60 {
61         return (uint16_t)(((x & 0x00ffU) << 8) |
62                 ((x & 0xff00U) >> 8));
63 }
64
65 /*
66  * An internal function to swap bytes in a 32-bit value.
67  *
68  * It is used by rte_bswap32() when the value is constant. Do not use
69  * this function directly; rte_bswap32() is preferred.
70  */
71 static inline uint32_t
72 rte_constant_bswap32(uint32_t x)
73 {
74         return  ((x & 0x000000ffUL) << 24) |
75                 ((x & 0x0000ff00UL) << 8) |
76                 ((x & 0x00ff0000UL) >> 8) |
77                 ((x & 0xff000000UL) >> 24);
78 }
79
80 /*
81  * An internal function to swap bytes of a 64-bit value.
82  *
83  * It is used by rte_bswap64() when the value is constant. Do not use
84  * this function directly; rte_bswap64() is preferred.
85  */
86 static inline uint64_t
87 rte_constant_bswap64(uint64_t x)
88 {
89         return  ((x & 0x00000000000000ffULL) << 56) |
90                 ((x & 0x000000000000ff00ULL) << 40) |
91                 ((x & 0x0000000000ff0000ULL) << 24) |
92                 ((x & 0x00000000ff000000ULL) <<  8) |
93                 ((x & 0x000000ff00000000ULL) >>  8) |
94                 ((x & 0x0000ff0000000000ULL) >> 24) |
95                 ((x & 0x00ff000000000000ULL) >> 40) |
96                 ((x & 0xff00000000000000ULL) >> 56);
97 }
98
99 /*
100  * An architecture-optimized byte swap for a 16-bit value.
101  *
102  * Do not use this function directly. The preferred function is rte_bswap16().
103  */
104 static inline uint16_t rte_arch_bswap16(uint16_t _x)
105 {
106         register uint16_t x = _x;
107         asm volatile ("xchgb %b[x1],%h[x2]"
108                       : [x1] "=Q" (x)
109                       : [x2] "0" (x)
110                       );
111         return x;
112 }
113
114 /*
115  * An architecture-optimized byte swap for a 32-bit value.
116  *
117  * Do not use this function directly. The preferred function is rte_bswap32().
118  */
119 static inline uint32_t rte_arch_bswap32(uint32_t _x)
120 {
121         register uint32_t x = _x;
122         asm volatile ("bswap %[x]"
123                       : [x] "+r" (x)
124                       );
125         return x;
126 }
127
128 /*
129  * An architecture-optimized byte swap for a 64-bit value.
130  *
131   * Do not use this function directly. The preferred function is rte_bswap64().
132  */
133 #ifdef RTE_ARCH_X86_64
134 /* 64-bit mode */
135 static inline uint64_t rte_arch_bswap64(uint64_t _x)
136 {
137         register uint64_t x = _x;
138         asm volatile ("bswap %[x]"
139                       : [x] "+r" (x)
140                       );
141         return x;
142 }
143 #else /* ! RTE_ARCH_X86_64 */
144 /* Compat./Leg. mode */
145 static inline uint64_t rte_arch_bswap64(uint64_t x)
146 {
147         uint64_t ret = 0;
148         ret |= ((uint64_t)rte_arch_bswap32(x & 0xffffffffUL) << 32);
149         ret |= ((uint64_t)rte_arch_bswap32((x >> 32) & 0xffffffffUL));
150         return ret;
151 }
152 #endif /* RTE_ARCH_X86_64 */
153
154
155 #ifndef RTE_FORCE_INTRINSICS
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 /**
164  * Swap bytes in a 32-bit value.
165  */
166 #define rte_bswap32(x) ((uint32_t)(__builtin_constant_p(x) ?            \
167                                    rte_constant_bswap32(x) :            \
168                                    rte_arch_bswap32(x)))
169
170 /**
171  * Swap bytes in a 64-bit value.
172  */
173 #define rte_bswap64(x) ((uint64_t)(__builtin_constant_p(x) ?            \
174                                    rte_constant_bswap64(x) :            \
175                                    rte_arch_bswap64(x)))
176
177 #else
178
179 /**
180  * Swap bytes in a 16-bit value.
181  * __builtin_bswap16 is only available gcc 4.8 and upwards
182  */
183 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)
184 #define rte_bswap16(x) __builtin_bswap16(x)
185 #else
186 #define rte_bswap16(x) ((uint16_t)(__builtin_constant_p(x) ?            \
187                                    rte_constant_bswap16(x) :            \
188                                    rte_arch_bswap16(x)))
189 #endif
190
191 /**
192  * Swap bytes in a 32-bit value.
193  */
194 #define rte_bswap32(x) __builtin_bswap32(x)
195
196 /**
197  * Swap bytes in a 64-bit value.
198  */
199 #define rte_bswap64(x) __builtin_bswap64(x)
200
201 #endif
202
203 /**
204  * Convert a 16-bit value from CPU order to little endian.
205  */
206 #define rte_cpu_to_le_16(x) (x)
207
208 /**
209  * Convert a 32-bit value from CPU order to little endian.
210  */
211 #define rte_cpu_to_le_32(x) (x)
212
213 /**
214  * Convert a 64-bit value from CPU order to little endian.
215  */
216 #define rte_cpu_to_le_64(x) (x)
217
218
219 /**
220  * Convert a 16-bit value from CPU order to big endian.
221  */
222 #define rte_cpu_to_be_16(x) rte_bswap16(x)
223
224 /**
225  * Convert a 32-bit value from CPU order to big endian.
226  */
227 #define rte_cpu_to_be_32(x) rte_bswap32(x)
228
229 /**
230  * Convert a 64-bit value from CPU order to big endian.
231  */
232 #define rte_cpu_to_be_64(x) rte_bswap64(x)
233
234
235 /**
236  * Convert a 16-bit value from little endian to CPU order.
237  */
238 #define rte_le_to_cpu_16(x) (x)
239
240 /**
241  * Convert a 32-bit value from little endian to CPU order.
242  */
243 #define rte_le_to_cpu_32(x) (x)
244
245 /**
246  * Convert a 64-bit value from little endian to CPU order.
247  */
248 #define rte_le_to_cpu_64(x) (x)
249
250
251 /**
252  * Convert a 16-bit value from big endian to CPU order.
253  */
254 #define rte_be_to_cpu_16(x) rte_bswap16(x)
255
256 /**
257  * Convert a 32-bit value from big endian to CPU order.
258  */
259 #define rte_be_to_cpu_32(x) rte_bswap32(x)
260
261 /**
262  * Convert a 64-bit value from big endian to CPU order.
263  */
264 #define rte_be_to_cpu_64(x) rte_bswap64(x)
265
266 #ifdef __cplusplus
267 }
268 #endif
269
270 #endif /* _RTE_BYTEORDER_H_ */