remove extra parentheses in return statement
[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 "generic/rte_byteorder.h"
46
47 /*
48  * An architecture-optimized byte swap for a 16-bit value.
49  *
50  * Do not use this function directly. The preferred function is rte_bswap16().
51  */
52 static inline uint16_t rte_arch_bswap16(uint16_t _x)
53 {
54         return (_x >> 8) | ((_x << 8) & 0xff00);
55 }
56
57 /*
58  * An architecture-optimized byte swap for a 32-bit value.
59  *
60  * Do not use this function directly. The preferred function is rte_bswap32().
61  */
62 static inline uint32_t rte_arch_bswap32(uint32_t _x)
63 {
64         return (_x >> 24) | ((_x >> 8) & 0xff00) | ((_x << 8) & 0xff0000) |
65                 ((_x << 24) & 0xff000000);
66 }
67
68 /*
69  * An architecture-optimized byte swap for a 64-bit value.
70  *
71   * Do not use this function directly. The preferred function is rte_bswap64().
72  */
73 /* 64-bit mode */
74 static inline uint64_t rte_arch_bswap64(uint64_t _x)
75 {
76         return (_x >> 56) | ((_x >> 40) & 0xff00) | ((_x >> 24) & 0xff0000) |
77                 ((_x >> 8) & 0xff000000) | ((_x << 8) & (0xffULL << 32)) |
78                 ((_x << 24) & (0xffULL << 40)) |
79                 ((_x << 40) & (0xffULL << 48)) | ((_x << 56));
80 }
81
82 #ifndef RTE_FORCE_INTRINSICS
83 #define rte_bswap16(x) ((uint16_t)(__builtin_constant_p(x) ?            \
84                                    rte_constant_bswap16(x) :            \
85                                    rte_arch_bswap16(x)))
86
87 #define rte_bswap32(x) ((uint32_t)(__builtin_constant_p(x) ?            \
88                                    rte_constant_bswap32(x) :            \
89                                    rte_arch_bswap32(x)))
90
91 #define rte_bswap64(x) ((uint64_t)(__builtin_constant_p(x) ?            \
92                                    rte_constant_bswap64(x) :            \
93                                    rte_arch_bswap64(x)))
94 #else
95 /*
96  * __builtin_bswap16 is only available gcc 4.8 and upwards
97  */
98 #if __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 8)
99 #define rte_bswap16(x) ((uint16_t)(__builtin_constant_p(x) ?            \
100                                    rte_constant_bswap16(x) :            \
101                                    rte_arch_bswap16(x)))
102 #endif
103 #endif
104
105 /* Power 8 have both little endian and big endian mode
106  * Power 7 only support big endian
107  */
108 #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
109
110 #define rte_cpu_to_le_16(x) (x)
111 #define rte_cpu_to_le_32(x) (x)
112 #define rte_cpu_to_le_64(x) (x)
113
114 #define rte_cpu_to_be_16(x) rte_bswap16(x)
115 #define rte_cpu_to_be_32(x) rte_bswap32(x)
116 #define rte_cpu_to_be_64(x) rte_bswap64(x)
117
118 #define rte_le_to_cpu_16(x) (x)
119 #define rte_le_to_cpu_32(x) (x)
120 #define rte_le_to_cpu_64(x) (x)
121
122 #define rte_be_to_cpu_16(x) rte_bswap16(x)
123 #define rte_be_to_cpu_32(x) rte_bswap32(x)
124 #define rte_be_to_cpu_64(x) rte_bswap64(x)
125
126 #else /* RTE_BIG_ENDIAN */
127
128 #define rte_cpu_to_le_16(x) rte_bswap16(x)
129 #define rte_cpu_to_le_32(x) rte_bswap32(x)
130 #define rte_cpu_to_le_64(x) rte_bswap64(x)
131
132 #define rte_cpu_to_be_16(x) (x)
133 #define rte_cpu_to_be_32(x) (x)
134 #define rte_cpu_to_be_64(x) (x)
135
136 #define rte_le_to_cpu_16(x) rte_bswap16(x)
137 #define rte_le_to_cpu_32(x) rte_bswap32(x)
138 #define rte_le_to_cpu_64(x) rte_bswap64(x)
139
140 #define rte_be_to_cpu_16(x) (x)
141 #define rte_be_to_cpu_32(x) (x)
142 #define rte_be_to_cpu_64(x) (x)
143 #endif
144
145 #ifdef __cplusplus
146 }
147 #endif
148
149 #endif /* _RTE_BYTEORDER_PPC_64_H_ */