4 * Copyright (C) IBM Corporation 2014.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
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.
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.
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.
38 #ifndef _RTE_BYTEORDER_PPC_64_H_
39 #define _RTE_BYTEORDER_PPC_64_H_
46 #include "generic/rte_byteorder.h"
49 * An architecture-optimized byte swap for a 16-bit value.
51 * Do not use this function directly. The preferred function is rte_bswap16().
53 static inline uint16_t rte_arch_bswap16(uint16_t _x)
55 return (_x >> 8) | ((_x << 8) & 0xff00);
59 * An architecture-optimized byte swap for a 32-bit value.
61 * Do not use this function directly. The preferred function is rte_bswap32().
63 static inline uint32_t rte_arch_bswap32(uint32_t _x)
65 return (_x >> 24) | ((_x >> 8) & 0xff00) | ((_x << 8) & 0xff0000) |
66 ((_x << 24) & 0xff000000);
70 * An architecture-optimized byte swap for a 64-bit value.
72 * Do not use this function directly. The preferred function is rte_bswap64().
75 static inline uint64_t rte_arch_bswap64(uint64_t _x)
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));
83 #ifndef RTE_FORCE_INTRINSICS
84 #define rte_bswap16(x) ((uint16_t)(__builtin_constant_p(x) ? \
85 rte_constant_bswap16(x) : \
88 #define rte_bswap32(x) ((uint32_t)(__builtin_constant_p(x) ? \
89 rte_constant_bswap32(x) : \
92 #define rte_bswap64(x) ((uint64_t)(__builtin_constant_p(x) ? \
93 rte_constant_bswap64(x) : \
97 * __builtin_bswap16 is only available gcc 4.8 and upwards
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)))
106 /* Power 8 have both little endian and big endian mode
107 * Power 7 only support big endian
109 #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
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)
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)
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)
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)
127 #else /* RTE_BIG_ENDIAN */
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)
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)
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)
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)
150 #endif /* _RTE_BYTEORDER_PPC_64_H_ */