Print this page
6729208 Optimize BSWAP_* and BE_* macros in sys/byteorder.h to use inline amd64 assembly
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/uts/common/sys/byteorder.h
+++ new/usr/src/uts/common/sys/byteorder.h
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21
22 22 /*
23 23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 24 * Use is subject to license terms.
25 25 */
26 26
27 27 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
28 28 /* All Rights Reserved */
29 29
30 30 /*
31 31 * University Copyright- Copyright (c) 1982, 1986, 1988
32 32 * The Regents of the University of California
33 33 * All Rights Reserved
34 34 *
35 35 * University Acknowledgment- Portions of this document are derived from
36 36 * software developed by the University of California, Berkeley, and its
37 37 * contributors.
38 38 */
39 39
40 40 #ifndef _SYS_BYTEORDER_H
41 41 #define _SYS_BYTEORDER_H
42 42
43 43 #include <sys/isa_defs.h>
44 44 #include <sys/int_types.h>
45 45
46 46 #if defined(__GNUC__) && defined(_ASM_INLINES) && \
47 47 (defined(__i386) || defined(__amd64))
48 48 #include <asm/byteorder.h>
49 49 #endif
50 50
51 51 #ifdef __cplusplus
52 52 extern "C" {
53 53 #endif
54 54
55 55 /*
56 56 * macros for conversion between host and (internet) network byte order
57 57 */
58 58
59 59 #if defined(_BIG_ENDIAN) && !defined(ntohl) && !defined(__lint)
60 60 /* big-endian */
61 61 #define ntohl(x) (x)
62 62 #define ntohll(x) (x)
63 63 #define ntohs(x) (x)
64 64 #define htonl(x) (x)
65 65 #define htonll(x) (x)
66 66 #define htons(x) (x)
67 67
68 68 #elif !defined(ntohl) /* little-endian */
69 69
70 70 #ifndef _IN_PORT_T
71 71 #define _IN_PORT_T
72 72 typedef uint16_t in_port_t;
73 73 #endif
74 74
75 75 #ifndef _IN_ADDR_T
76 76 #define _IN_ADDR_T
77 77 typedef uint32_t in_addr_t;
78 78 #endif
79 79
80 80 #if !defined(_XPG4_2) || defined(__EXTENSIONS__) || defined(_XPG5)
81 81 extern uint32_t htonl(uint32_t);
82 82 extern uint16_t htons(uint16_t);
83 83 extern uint32_t ntohl(uint32_t);
84 84 extern uint16_t ntohs(uint16_t);
85 85 #else
86 86 extern in_addr_t htonl(in_addr_t);
87 87 extern in_port_t htons(in_port_t);
88 88 extern in_addr_t ntohl(in_addr_t);
89 89 extern in_port_t ntohs(in_port_t);
90 90 #endif /* !defined(_XPG4_2) || defined(__EXTENSIONS__) || defined(_XPG5) */
91 91 #if !(defined(_XPG4_2) || defined(_XPG5)) || defined(__EXTENSIONS__)
92 92 extern uint64_t htonll(uint64_t);
↓ open down ↓ |
92 lines elided |
↑ open up ↑ |
93 93 extern uint64_t ntohll(uint64_t);
94 94 #endif /* !(_XPG4_2||_XPG5) || __EXTENSIONS__ */
95 95 #endif
96 96
97 97 #if !defined(_XPG4_2) || defined(__EXTENSIONS__)
98 98
99 99 /*
100 100 * Macros to reverse byte order
101 101 */
102 102 #define BSWAP_8(x) ((x) & 0xff)
103 +#if !defined(__i386) && !defined(__amd64)
103 104 #define BSWAP_16(x) ((BSWAP_8(x) << 8) | BSWAP_8((x) >> 8))
104 -#define BSWAP_32(x) ((BSWAP_16(x) << 16) | BSWAP_16((x) >> 16))
105 -#define BSWAP_64(x) ((BSWAP_32(x) << 32) | BSWAP_32((x) >> 32))
105 +#define BSWAP_32(x) (((uint32_t)(x) << 24) | \
106 + (((uint32_t)(x) << 8) & 0xff0000) | \
107 + (((uint32_t)(x) >> 8) & 0xff00) | \
108 + ((uint32_t)(x) >> 24))
109 +#else /* x86 */
110 +#define BSWAP_16(x) htons(x)
111 +#define BSWAP_32(x) htonl(x)
112 +#endif /* !__i386 && !__amd64 */
106 113
114 +#if (!defined(__i386) && !defined(__amd64)) || \
115 + ((defined(_XPG4_2) || defined(_XPG5)) && !defined(__EXTENSIONS__))
116 +#define BSWAP_64(x) (((uint64_t)(x) << 56) | \
117 + (((uint64_t)(x) << 40) & 0xff000000000000ULL) | \
118 + (((uint64_t)(x) << 24) & 0xff0000000000ULL) | \
119 + (((uint64_t)(x) << 8) & 0xff00000000ULL) | \
120 + (((uint64_t)(x) >> 8) & 0xff000000ULL) | \
121 + (((uint64_t)(x) >> 24) & 0xff0000ULL) | \
122 + (((uint64_t)(x) >> 40) & 0xff00ULL) | \
123 + ((uint64_t)(x) >> 56))
124 +#else /* x86 with non-XPG extensions allowed */
125 +#define BSWAP_64(x) htonll(x)
126 +#endif /* (!__i386&&!__amd64) || ((_XPG4_2||_XPG5) && !__EXTENSIONS__) */
127 +
107 128 #define BMASK_8(x) ((x) & 0xff)
108 129 #define BMASK_16(x) ((x) & 0xffff)
109 130 #define BMASK_32(x) ((x) & 0xffffffff)
110 131 #define BMASK_64(x) (x)
111 132
112 133 /*
113 134 * Macros to convert from a specific byte order to/from native byte order
114 135 */
115 136 #ifdef _BIG_ENDIAN
116 137 #define BE_8(x) BMASK_8(x)
117 138 #define BE_16(x) BMASK_16(x)
118 139 #define BE_32(x) BMASK_32(x)
119 140 #define BE_64(x) BMASK_64(x)
120 141 #define LE_8(x) BSWAP_8(x)
121 142 #define LE_16(x) BSWAP_16(x)
122 143 #define LE_32(x) BSWAP_32(x)
123 144 #define LE_64(x) BSWAP_64(x)
124 145 #else
125 146 #define LE_8(x) BMASK_8(x)
126 147 #define LE_16(x) BMASK_16(x)
127 148 #define LE_32(x) BMASK_32(x)
128 149 #define LE_64(x) BMASK_64(x)
129 150 #define BE_8(x) BSWAP_8(x)
130 151 #define BE_16(x) BSWAP_16(x)
131 152 #define BE_32(x) BSWAP_32(x)
132 153 #define BE_64(x) BSWAP_64(x)
↓ open down ↓ |
16 lines elided |
↑ open up ↑ |
133 154 #endif
134 155
135 156 /*
136 157 * Macros to read unaligned values from a specific byte order to
137 158 * native byte order
138 159 */
139 160
140 161 #define BE_IN8(xa) \
141 162 *((uint8_t *)(xa))
142 163
164 +#if !defined(__i386) && !defined(__amd64)
143 165 #define BE_IN16(xa) \
144 - (((uint16_t)BE_IN8(xa) << 8) | BE_IN8((uint8_t *)(xa)+1))
166 + (((uint16_t)BE_IN8(xa) << 8) | BE_IN8((uint8_t *)(xa) + 1))
145 167
146 168 #define BE_IN32(xa) \
147 - (((uint32_t)BE_IN16(xa) << 16) | BE_IN16((uint8_t *)(xa)+2))
169 + (((uint32_t)BE_IN16(xa) << 16) | BE_IN16((uint8_t *)(xa) + 2))
148 170
171 +#else /* x86 */
172 +#define BE_IN16(xa) htons(*((uint16_t *)(void *)(xa)))
173 +#define BE_IN32(xa) htonl(*((uint32_t *)(void *)(xa)))
174 +#endif /* !__i386 && !__amd64 */
175 +
176 +#if (!defined(__i386) && !defined(__amd64)) || \
177 + ((defined(_XPG4_2) || defined(_XPG5)) && !defined(__EXTENSIONS__))
149 178 #define BE_IN64(xa) \
150 - (((uint64_t)BE_IN32(xa) << 32) | BE_IN32((uint8_t *)(xa)+4))
179 + (((uint64_t)BE_IN32(xa) << 32) | BE_IN32((uint8_t *)(xa) + 4))
180 +#else /* x86 with non-XPG extensions allowed */
181 +#define BE_IN64(xa) htonll(*((uint64_t *)(void *)(xa)))
182 +#endif /* (!__i386&&!__amd64) || ((_XPG4_2||_XPG5) && !__EXTENSIONS__) */
151 183
152 184 #define LE_IN8(xa) \
153 185 *((uint8_t *)(xa))
154 186
155 187 #define LE_IN16(xa) \
156 188 (((uint16_t)LE_IN8((uint8_t *)(xa) + 1) << 8) | LE_IN8(xa))
157 189
158 190 #define LE_IN32(xa) \
159 191 (((uint32_t)LE_IN16((uint8_t *)(xa) + 2) << 16) | LE_IN16(xa))
160 192
161 193 #define LE_IN64(xa) \
162 194 (((uint64_t)LE_IN32((uint8_t *)(xa) + 4) << 32) | LE_IN32(xa))
163 195
164 196 /*
165 197 * Macros to write unaligned values from native byte order to a specific byte
166 198 * order.
167 199 */
168 200
↓ open down ↓ |
8 lines elided |
↑ open up ↑ |
169 201 #define BE_OUT8(xa, yv) *((uint8_t *)(xa)) = (uint8_t)(yv);
170 202
171 203 #define BE_OUT16(xa, yv) \
172 204 BE_OUT8((uint8_t *)(xa) + 1, yv); \
173 205 BE_OUT8((uint8_t *)(xa), (yv) >> 8);
174 206
175 207 #define BE_OUT32(xa, yv) \
176 208 BE_OUT16((uint8_t *)(xa) + 2, yv); \
177 209 BE_OUT16((uint8_t *)(xa), (yv) >> 16);
178 210
211 +#if (!defined(__i386) && !defined(__amd64)) || \
212 + ((defined(_XPG4_2) || defined(_XPG5)) && !defined(__EXTENSIONS__))
179 213 #define BE_OUT64(xa, yv) \
180 214 BE_OUT32((uint8_t *)(xa) + 4, yv); \
181 215 BE_OUT32((uint8_t *)(xa), (yv) >> 32);
216 +#else /* x86 with non-XPG extensions allowed */
217 +#define BE_OUT64(xa, yv) *((uint64_t *)(void *)(xa)) = htonll((uint64_t)(yv));
218 +#endif /* (!__i386&&!__amd64) || ((_XPG4_2||_XPG5) && !__EXTENSIONS__) */
182 219
183 220 #define LE_OUT8(xa, yv) *((uint8_t *)(xa)) = (uint8_t)(yv);
184 221
185 222 #define LE_OUT16(xa, yv) \
186 223 LE_OUT8((uint8_t *)(xa), yv); \
187 224 LE_OUT8((uint8_t *)(xa) + 1, (yv) >> 8);
188 225
189 226 #define LE_OUT32(xa, yv) \
190 227 LE_OUT16((uint8_t *)(xa), yv); \
191 228 LE_OUT16((uint8_t *)(xa) + 2, (yv) >> 16);
192 229
193 230 #define LE_OUT64(xa, yv) \
194 231 LE_OUT32((uint8_t *)(xa), yv); \
195 232 LE_OUT32((uint8_t *)(xa) + 4, (yv) >> 32);
196 233
197 234 #endif /* !defined(_XPG4_2) || defined(__EXTENSIONS__) */
198 235
199 236 #ifdef __cplusplus
200 237 }
201 238 #endif
202 239
203 240 #endif /* _SYS_BYTEORDER_H */
↓ open down ↓ |
12 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX