Imported Upstream version 16.11
[deb_dpdk.git] / lib / librte_eal / common / include / arch / ppc_64 / rte_byteorder.h
1 /*
2  *   BSD LICENSE
3  *
4  *   Copyright (C) IBM Corporation 2014.
5  *
6  *   Redistribution and use in source and binary forms, with or without
7  *   modification, are permitted provided that the following conditions
8  *   are met:
9  *
10  *     * Redistributions of source code must retain the above copyright
11  *       notice, this list of conditions and the following disclaimer.
12  *     * Redistributions in binary form must reproduce the above copyright
13  *       notice, this list of conditions and the following disclaimer in
14  *       the documentation and/or other materials provided with the
15  *       distribution.
16  *     * Neither the name of IBM Corporation nor the names of its
17  *       contributors may be used to endorse or promote products derived
18  *       from this software without specific prior written permission.
19  *
20  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /* Inspired from FreeBSD src/sys/powerpc/include/endian.h
34  * Copyright (c) 1987, 1991, 1993
35  * The Regents of the University of California.  All rights reserved.
36 */
37
38 #ifndef _RTE_BYTEORDER_PPC_64_H_
39 #define _RTE_BYTEORDER_PPC_64_H_
40
41 #ifdef __cplusplus
42 extern "C" {
43 #endif
44
45 #include <stdint.h>
46 #include "generic/rte_byteorder.h"
47
48 /*
49  * An architecture-optimized byte swap for a 16-bit value.
50  *
51  * Do not use this function directly. The preferred function is rte_bswap16().
52  */
53 static inline uint16_t rte_arch_bswap16(uint16_t _x)
54 {
55         return (_x >> 8) | ((_x << 8) & 0xff00);
56 }
57
58 /*
59  * An architecture-optimized byte swap for a 32-bit value.
60  *
61  * Do not use this function directly. The preferred function is rte_bswap32().
62  */
63 static inline uint32_t rte_arch_bswap32(uint32_t _x)
64 {
65         return (_x >> 24) | ((_x >> 8) & 0xff00) | ((_x << 8) & 0xff0000) |
66                 ((_x << 24) & 0xff000000);
67 }
68
69 /*
70  * An architecture-optimized byte swap for a 64-bit value.
71  *
72   * Do not use this function directly. The preferred function is rte_bswap64().
73  */
74 /* 64-bit mode */
75 static inline uint64_t rte_arch_bswap64(uint64_t _x)
76 {
77         return (_x >> 56) | ((_x >> 40) & 0xff00) | ((_x >> 24) & 0xff0000) |
78                 ((_x >> 8) & 0xff000000) | ((_x << 8) & (0xffULL << 32)) |
79                 ((_x << 24) & (0xffULL << 40)) |
80                 ((_x << 40) & (0xffULL << 48)) | ((_x << 56));
81 }
82
83 #ifndef RTE_FORCE_INTRINSICS
84 #define rte_bswap16(x) ((uint16_t)(__builtin_constant_p(x) ?            \
85                                    rte_constant_bswap16(x) :            \
86                                    rte_arch_bswap16(x)))
87
88 #define rte_bswap32(x) ((uint32_t)(__builtin_constant_p(x) ?            \
89                                    rte_constant_bswap32(x) :            \
90                                    rte_arch_bswap32(x)))
91
92 #define rte_bswap64(x) ((uint64_t)(__builtin_constant_p(x) ?            \
93                                    rte_constant_bswap64(x) :            \
94                                    rte_arch_bswap64(x)))
95 #else
96 /*
97  * __builtin_bswap16 is only available gcc 4.8 and upwards
98  */
99 #if __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 8)
100 #define rte_bswap16(x) ((uint16_t)(__builtin_constant_p(x) ?            \
101                                    rte_constant_bswap16(x) :            \
102                                    rte_arch_bswap16(x)))
103 #endif
104 #endif
105
106 /* Power 8 have both little endian and big endian mode
107  * Power 7 only support big endian
108  */
109 #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
110
111 #define rte_cpu_to_le_16(x) (x)
112 #define rte_cpu_to_le_32(x) (x)
113 #define rte_cpu_to_le_64(x) (x)
114
115 #define rte_cpu_to_be_16(x) rte_bswap16(x)
116 #define rte_cpu_to_be_32(x) rte_bswap32(x)
117 #define rte_cpu_to_be_64(x) rte_bswap64(x)
118
119 #define rte_le_to_cpu_16(x) (x)
120 #define rte_le_to_cpu_32(x) (x)
121 #define rte_le_to_cpu_64(x) (x)
122
123 #define rte_be_to_cpu_16(x) rte_bswap16(x)
124 #define rte_be_to_cpu_32(x) rte_bswap32(x)
125 #define rte_be_to_cpu_64(x) rte_bswap64(x)
126
127 #else /* RTE_BIG_ENDIAN */
128
129 #define rte_cpu_to_le_16(x) rte_bswap16(x)
130 #define rte_cpu_to_le_32(x) rte_bswap32(x)
131 #define rte_cpu_to_le_64(x) rte_bswap64(x)
132
133 #define rte_cpu_to_be_16(x) (x)
134 #define rte_cpu_to_be_32(x) (x)
135 #define rte_cpu_to_be_64(x) (x)
136
137 #define rte_le_to_cpu_16(x) rte_bswap16(x)
138 #define rte_le_to_cpu_32(x) rte_bswap32(x)
139 #define rte_le_to_cpu_64(x) rte_bswap64(x)
140
141 #define rte_be_to_cpu_16(x) (x)
142 #define rte_be_to_cpu_32(x) (x)
143 #define rte_be_to_cpu_64(x) (x)
144 #endif
145
146 #ifdef __cplusplus
147 }
148 #endif
149
150 #endif /* _RTE_BYTEORDER_PPC_64_H_ */