lib: use SPDX tag for Intel copyright files
[dpdk.git] / lib / librte_eal / common / include / arch / x86 / rte_byteorder.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4
5 #ifndef _RTE_BYTEORDER_X86_H_
6 #define _RTE_BYTEORDER_X86_H_
7
8 #ifdef __cplusplus
9 extern "C" {
10 #endif
11
12 #include <stdint.h>
13 #include <rte_common.h>
14 #include "generic/rte_byteorder.h"
15
16 #ifndef RTE_BYTE_ORDER
17 #define RTE_BYTE_ORDER RTE_LITTLE_ENDIAN
18 #endif
19
20 /*
21  * An architecture-optimized byte swap for a 16-bit value.
22  *
23  * Do not use this function directly. The preferred function is rte_bswap16().
24  */
25 static inline uint16_t rte_arch_bswap16(uint16_t _x)
26 {
27         register uint16_t x = _x;
28         asm volatile ("xchgb %b[x1],%h[x2]"
29                       : [x1] "=Q" (x)
30                       : [x2] "0" (x)
31                       );
32         return x;
33 }
34
35 /*
36  * An architecture-optimized byte swap for a 32-bit value.
37  *
38  * Do not use this function directly. The preferred function is rte_bswap32().
39  */
40 static inline uint32_t rte_arch_bswap32(uint32_t _x)
41 {
42         register uint32_t x = _x;
43         asm volatile ("bswap %[x]"
44                       : [x] "+r" (x)
45                       );
46         return x;
47 }
48
49 #ifndef RTE_FORCE_INTRINSICS
50 #define rte_bswap16(x) ((uint16_t)(__builtin_constant_p(x) ?            \
51                                    rte_constant_bswap16(x) :            \
52                                    rte_arch_bswap16(x)))
53
54 #define rte_bswap32(x) ((uint32_t)(__builtin_constant_p(x) ?            \
55                                    rte_constant_bswap32(x) :            \
56                                    rte_arch_bswap32(x)))
57
58 #define rte_bswap64(x) ((uint64_t)(__builtin_constant_p(x) ?            \
59                                    rte_constant_bswap64(x) :            \
60                                    rte_arch_bswap64(x)))
61 #else
62 /*
63  * __builtin_bswap16 is only available gcc 4.8 and upwards
64  */
65 #if __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 8)
66 #define rte_bswap16(x) ((uint16_t)(__builtin_constant_p(x) ?            \
67                                    rte_constant_bswap16(x) :            \
68                                    rte_arch_bswap16(x)))
69 #endif
70 #endif
71
72 #define rte_cpu_to_le_16(x) (x)
73 #define rte_cpu_to_le_32(x) (x)
74 #define rte_cpu_to_le_64(x) (x)
75
76 #define rte_cpu_to_be_16(x) rte_bswap16(x)
77 #define rte_cpu_to_be_32(x) rte_bswap32(x)
78 #define rte_cpu_to_be_64(x) rte_bswap64(x)
79
80 #define rte_le_to_cpu_16(x) (x)
81 #define rte_le_to_cpu_32(x) (x)
82 #define rte_le_to_cpu_64(x) (x)
83
84 #define rte_be_to_cpu_16(x) rte_bswap16(x)
85 #define rte_be_to_cpu_32(x) rte_bswap32(x)
86 #define rte_be_to_cpu_64(x) rte_bswap64(x)
87
88 #ifdef RTE_ARCH_I686
89 #include "rte_byteorder_32.h"
90 #else
91 #include "rte_byteorder_64.h"
92 #endif
93
94 #ifdef __cplusplus
95 }
96 #endif
97
98 #endif /* _RTE_BYTEORDER_X86_H_ */